In this
article I will show you how you can how you can access dropdownlist and
dropdownlistfor value from view to controller in mvc, c#.net. I will show separate
dropdownlist and dropdownlistfor selected value.
Some of my
previous articles are as follow: Dynamically
Load Asp.net MVC Layout Page On DropDownList Value Using C#.Net, jQuery
DatePicker Calendar With Dropdown Month and Year in Asp.Net, Display
DropdownList Item By Group in Asp.Net MVC Using C#.Net, Bind
DropDownList Using Entity Framework in ASP.Net MVC Using C#, Bind
DropDownList In MVC WebGrid and Retrive Value Using Asp.net MVC, C#.Net, MultiSelect
DropdownList Using jQuery in Asp.Net MVC and C#.Net, DropdownList
AutoPostback Functionality In MVC Using C#.Net.
So for this article first we will create a new asp.net application and add a model class file. After adding class file we will add the below code into it.
So for this article first we will create a new asp.net application and add a model class file. After adding class file we will add the below code into it.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MVC_Demos.Models
{
public class StudentModel
{
public List<Student> StudentList {
get; set; }
public int SelectedStudentId { get; set; }
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
}
}
|
After this
we will add a controller class file. In this we will create some list value for
showing in dropdown list.
public ActionResult
Index()
{
StudentModel
_studentmodel = new StudentModel();
_studentmodel.StudentList = new List<Student>();
_studentmodel.StudentList.Add(new Student { Id = 1, Name = "Student1" });
_studentmodel.StudentList.Add(new Student { Id = 2, Name = "Student2" });
_studentmodel.StudentList.Add(new Student { Id = 3, Name = "Student3" });
return
View(_studentmodel);
}
|
Now first
we will check for
DropDownListFor:
///
/// Example for DropDownList
///
///
[HttpPost]
public ActionResult Index(StudentModel _studentmodel)
{
_studentmodel.StudentList = new List<Student>();
_studentmodel.StudentList.Add(new Student { Id = 1, Name = "Student1" });
_studentmodel.StudentList.Add(new Student { Id = 2, Name = "Student2" });
_studentmodel.StudentList.Add(new Student { Id = 3, Name = "Student3" });
return View(_studentmodel);
}
|
Here in
above code I have passed the student model class as parameter. Now generate the
view and add the below code.
@model MVC_Demos.Models.StudentModel
@{
ViewBag.Title
= "Index";
}
@using (Html.BeginForm("Index", "DropDownList"))
{
<b>Example Of
DropDownListFor </b><br />
@Html.DropDownListFor(m=>m.SelectedStudentId,
new SelectList(Model.StudentList,
"Id", "Name"), "Select Name")<br />
<input type="submit" value="Submit" />
}
|
In above code I have bind the dropdownlistfor with model. Now run it and check the output.
DropDownList:
For this
add the below code.
///
/// Example for DropDownList
///
///
[HttpPost]
public ActionResult Index(string SelectedStudentId)
{
StudentModel _studentmodel = new StudentModel();
_studentmodel.StudentList = new List<Student>();
_studentmodel.StudentList.Add(new Student { Id = 1, Name = "Student1" });
_studentmodel.StudentList.Add(new Student { Id = 2, Name = "Student2" });
_studentmodel.StudentList.Add(new Student { Id = 3, Name = "Student3" });
return View(_studentmodel);
}
|
Now we will
create view and add the below code.
@model MVC_Demos.Models.StudentModel
@{
ViewBag.Title
= "Index";
}
@using (Html.BeginForm("Index", "DropDownList"))
{
<b>Example Of
DropDownList </b><br />
@Html.DropDownList("SelectedStudentId", new SelectList(Model.StudentList, "Id", "Name"), "Select Name")<br />
<input type="submit" value="Submit" />
}
|
Here in above code you can check the name of dropdownlist is same as the parameter passed into the index post method.
Now run it
and check the output.
great post
ReplyDeleteThank You For Sharing