MVC and Dropdownliast
-
hi, do you know how can I add dropdown list?
<%Html.DropDownList(??????????)%>
-
hi, do you know how can I add dropdown list?
<%Html.DropDownList(??????????)%>
Using the DropDownList Helper The DropDownList helper renders a drop-down list. In its simplest form, DropDownList takes one parameter, the name of the ViewData key whose value is of type SelectList and that contains the option values for the drop-down list. The MVC framework uses the ModelState property of ViewData to determine the selected value. If the ModelState property is empty, the framework looks for an item whose Selected property is set. The following example shows markup for the DropDownList helper method.
<%= Html.DropDownList("pets") %>
The following code is a part of an Index action method in which values are added to a List object. The List object is passed to an instance of SelectList, which is then added to the ViewData object.
List<string> petList =
new List<string>()
;
petList.Add("Dog");
petList.Add("Cat");
petList.Add("Hamster");
petList.Add("Parrot");
petList.Add("Gold fish");
petList.Add("Mountain lion");
petList.Add("Elephant");ViewData["Pets"] = new SelectList(petList);
Note:**Both the DropDownList and ListBox helpers accept either a SelectList or MultiSelectList object. have a look on this link Click Here[^]