Various Ways To Use DropdownList Control In MVC Application

In this article I am showing you how to use DropDownList control in MVC and how to bind the data to DropDown control and list control.

Here we will answer the following questions: 

How to bind the enums in the DropDown control? How to use the list box in DropDown?

Method 1: Using View Bag specify selected list name:

For this you have to bind the data to view bag. By giving reference of name parameter of selected list, dropdown control gets filled in with the data given to the view bag.

The following are the parameters you have to pass:
  1. // Summary:  
  2. // Returns a single-selection select element using the specified HTML helper and  
  3. // the name of the form field.  
  4. //  
  5. // Parameters:  
  6. // htmlHelper:  
  7. // The HTML helper instance that this method extends.  
  8. //  
  9. // name:  
  10. // The name of the form field to return.  
  11. //  
  12. // Returns:  
  13. // An HTML select element.  
  14. //  
  15. // Exceptions:  
  16. // T:System.ArgumentException:  
  17. // The name parameter is null or empty.  
  18. public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name);  
In controller bind selected list to the view bag as in the following code snippet:

view bag

In view pass the name of view bag element . You will get DropDown list filled:

student list

DropDown will be displayed like the following. But remember there won’t be any select parameter:

dropdown list

To specify the select parameter you need to do small change, pass extra parameter. As below specify the default option:
  1. // name:  
  2. // The name of the form field to return.  
  3. //  
  4. // optionLabel:  
  5. // The text for a default empty item. This parameter can be null.  
  6. public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, string optionLabel);  
In view specify the default option as in the following code snippet:

dropdownlist2

It will display the dropdown with default option as below:

select

Method 2: Specify the selected list in view bag, ID and value:

In the method you have to specify the selected list and value in parameter as below. You may also use the model element:
  1. // name:  
  2. // The name of the form field to return.  
  3. //  
  4. // selectList:  
  5. // A collection of System.Web.Mvc.SelectListItem objects that are used to populate  
  6. // the drop-down list.  
  7. //  
  8. // Returns:  
  9. // An HTML select element with an option subelement for each item in the list.  
  10. //  
  11. // Exceptions:  
  12. // T:System.ArgumentException:  
  13. // The name parameter is null or empty.  
  14. public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable < SelectListItem > selectList);  
In view as below:

view bag

DropDown will display like the following. But remember there won’t be any select parameter:

testname

To provide additional select parameter you need to do small change:

In the method you have to specify the selected list and value in parameter as below. You may also use the model element in this option: 
  1. // name:  
  2. // The name of the form field to return.  
  3. //  
  4. // selectList:  
  5. // A collection of System.Web.Mvc.SelectListItem objects that are used to populate  
  6. // the drop-down list.  
  7. //  
  8. // optionLabel:  
  9. // The text for a default empty item. This parameter can be null.  
  10. public static MvcHtmlString DropDownList
  11.    (this HtmlHelper htmlHelper, string name, IEnumerable < SelectListItem > selectList, string optionLabel);  
In view as below:

It will display the DropDown with default option as below:

please select

Method 3: Displaying data from model:

To display the data from model element you need to simply provide expression and ID and value element in it.

You may also specify the select parameter to it as above:
  1. // expression:  
  2. // An expression that identifies the object that contains the properties to display.  
  3. //  
  4. // selectList:  
  5. // A collection of System.Web.Mvc.SelectListItem objects that are used to populate  
  6. // the drop-down list.  
  7. public static MvcHtmlString DropDownListFor < TModel, TProperty > (this HtmlHelper < TModel > htmlHelper, Expression < Func < TModel, TPr       operty >> expression, IEnumerable < SelectListItem > selectList);  
  8. public static MvcHtmlString DropDownListFor < TModel, TProperty > (this HtmlHelper < TModel > htmlHelper, Expression < Func < TModel, TPr       operty >> expression, IEnumerable < SelectListItem > selectList, string optionLabel);  
Additionally you can also specify the HTML attributes to it. You can also  apply the CSS attributes.
  1. public static MvcHtmlString DropDownListFor  
  2. <TModel, TProperty>(this HtmlHelper  
  3. <TModel> htmlHelper, Expression  
  4.     <Func  
  5.        <TModel, TProperty>> expression, IEnumerable  
  6.            <SelectListItem> selectList, string optionLabel, object htmlAttributes);  
  7.              @Html.DropDownListFor(M => M.Studentmodel, new SelectList(Model.Studentmodel, "ID""Name", 0))  
  8.                <br>  
  9.                <br>  
  10.                   @Html.DropDownListFor(M => M.Studentmodel, new SelectList(Model.Studentmodel, "ID""Name", 0), "Please Select")  
  11.                <br>  
  12.                <br>  
  13.                   @Html.DropDownListFor(M => M.Studentmodel, new SelectList(Model.Studentmodel, "ID""Name", 0), "Please Select"new { @id = "StudentDropDown" })  
  14.                <br>  
  15.             <br>  
Method 4: Converting the enum value to dropdown value:

enum

To bind this value to DropDown you must use @Html.EnumDropDownListFor.

In EnumDropdownListFor you may also use the selected element as the preceding DropDown boxes we use:
  1. @Html.EnumDropDownListFor(M => M.CountryNames)  
  2. <br>  
  3.     <br>  
  4.         @Html.EnumDropDownListFor(M => M.CountryNames,"Select Country")  
  5.     <br>  
  6.     <br>  
  7.         @Html.EnumDropDownListFor(M => M.CountryNames, "Select Country"new { @id = "StudentDropDown" })  
  8.     <br>  
  9. <br>  
Method 5 : Display the multiselect list:

As per the following we display the DropDown list:
  1. Using data from viewbag:
    1. @Html.ListBox("StrudentList")  
    2. <br> <br>    
  2. Provide the selected list: 
    1. @Html.ListBox("StrudentList1"new SelectList(Model.Studentmodel, "ID""Name", 0), new  
    2. {  
    3.     Multiple = "multiple"  
    4. })  
    5. < br >  
    6. < br >   
    7. @Html.ListBoxFor(M => M.Studentmodel, new SelectList(Model.Studentmodel, "ID""Name", 0), new  
    8. {  
    9.     Multiple = "multiple"  
    10. }  
    testname123

    Hope you understood how to use dropdownlist control in various ways in MVC. In the project code attached use Get Students controller Index as page i.e. http://localhost:49681/GetStudents/Index.