Suggested Videos
Part 31 - Using custom view engines
Part 32 - How does a controller find a view
Part 33 - Html helpers
To generate a dropdownlist, use DropDownList html helper. A dropdownlist in MVC is a collection of SelectListItem objects. Depending on your project requirement you may either hard code the values in code or retrieve them from a database table. In this video, we will discuss both the approaches.
Generating a dropdownlist using hard coded values: We will use the following overloaded version of "DropDownList" html helper.
DropDownList(string name, IEnumerable<SelectListItem> selectList, string optionLabel)
The following code will generate a departments dropdown list. The first item in the list will be "Select Department".
@Html.DropDownList("Departments", new List<SelectListItem>
{
new SelectListItem { Text = "IT", Value = "1", Selected=true},
new SelectListItem { Text = "HR", Value = "2"},
new SelectListItem { Text = "Payroll", Value = "3"}
}, "Select Department")
The downside of hard coding dropdownlist values with-in code is that, if we have to add or remove departments from the dropdownlist, the code needs to be modified.
In most cases, we get values from the database table. For this example, let's use entity framework to retrieve data. Add ADO.NET entity data model. We discussed working with entity framework in Part 8 & Part 25.
To pass list of Departments from the controller, store them in "ViewBag"
public ActionResult Index()
{
// Connect to the database
SampleDBContext db = new SampleDBContext();
// Retrieve departments, and build SelectList
ViewBag.Departments = new SelectList(db.Departments, "Id", "Name");
return View();
}
Now in the "Index" view, access Departments list from "ViewBag"
@Html.DropDownList("Departments", "Select Department")
Part 31 - Using custom view engines
Part 32 - How does a controller find a view
Part 33 - Html helpers
To generate a dropdownlist, use DropDownList html helper. A dropdownlist in MVC is a collection of SelectListItem objects. Depending on your project requirement you may either hard code the values in code or retrieve them from a database table. In this video, we will discuss both the approaches.
Generating a dropdownlist using hard coded values: We will use the following overloaded version of "DropDownList" html helper.
DropDownList(string name, IEnumerable<SelectListItem> selectList, string optionLabel)
The following code will generate a departments dropdown list. The first item in the list will be "Select Department".
@Html.DropDownList("Departments", new List<SelectListItem>
{
new SelectListItem { Text = "IT", Value = "1", Selected=true},
new SelectListItem { Text = "HR", Value = "2"},
new SelectListItem { Text = "Payroll", Value = "3"}
}, "Select Department")
The downside of hard coding dropdownlist values with-in code is that, if we have to add or remove departments from the dropdownlist, the code needs to be modified.
In most cases, we get values from the database table. For this example, let's use entity framework to retrieve data. Add ADO.NET entity data model. We discussed working with entity framework in Part 8 & Part 25.
To pass list of Departments from the controller, store them in "ViewBag"
public ActionResult Index()
{
// Connect to the database
SampleDBContext db = new SampleDBContext();
// Retrieve departments, and build SelectList
ViewBag.Departments = new SelectList(db.Departments, "Id", "Name");
return View();
}
Now in the "Index" view, access Departments list from "ViewBag"
@Html.DropDownList("Departments", "Select Department")
Hello Venkat,
ReplyDeleteCould you please upload how to do a Editable dropdown list in MVC.
Thank you,
MVS
how can pass dropdown selected item to controller from veiw
ReplyDeleteSir pls upload video C# 3.0 and C# 4.0 new concept like Lamda expression , Extension Method etc bc'z these concept use by your self throuhg out your all preveious demo, any many student confuse them pls pls pls as soon as possible upload this
ReplyDeleteHow Helper know need to get `Departments` from the ViewBag?
ReplyDeleteSir pls guide me I want Multicolumn Dropdown list from table Example RollNo, StudentName, Location in View. Sir Its very urgent so please help me
ReplyDeletehow to make SelectionChangeEvent with getting ID?
ReplyDeleteAn exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code
ReplyDeleteAdditional information: There is no ViewData item of type 'IEnumerable' that has the key 'Departments'.
How to create cascading dropdown list control for Country -> State -> City
ReplyDelete