Various ways to show DropDownList (select element in HTML) in MVC
Introduction
In Webform application, there are many drag and drop controls for dropdownlist. But in MVC creating dropdownlist is totally different. In MVC HtmlHelper provides different extension method to bind or populate the dropdownlist in view.
There three extension methods of HtmlHelper as follows
- Html.DropDownList
- Html.DropDownListFor
- Html.EnumDropDownListFor
Prerequisites
- need to know how to pass model to the view
- Basic knowledge of MVC Architecture
Html.DropDownList
It returns a select HTML element that lets users select one item. You can use in various ways
Using Model
Using ViewBag
Using ViewData
Hardcode values on View
Html.DropDownListFor
It returns an HTML select element for each property in the object that is represented by the specified expression. It can be used for stongly typed view.
You can use in various ways
Using Model
Html.EnumDropDownListFor
It returns an HTML select element for each value in the enumeration that is represented by the specified expression.
Conclusion
From above description, I hope you can able to use different form of dropdownlist.