In this article, I am going to discuss DropDownList HTML Helper in ASP.NET MVC Application. Please read our previous article before proceeding to this article where we discussed How to Create TextBox and TextArea using HTML Helper Methods in MVC Application. In order to generate a DropDownList in ASP.NET MVC Application, we need to use the DropDownList HTML Helper Method. As part of this article, we are going to discuss the following pointers.
- What is a DropDownList?
- Understanding DropDownList() HTML Helper Method in ASP.NET MVC.
- How to set the Dropdown list values from the database in the ASP.NET MVC Application?
- How to use Enum to set the Dropdown list values in MVC?
- Understanding DropDownListFor HTML Helper in ASP.NET MVC Application.
What is a DropDownList?
A DropDownList in ASP.NET MVC application is nothing but a collection of SelectListItem objects. Depending on your business requirement you may either hard code the values or you may retrieve the values from a database table. In this Video, I am going to discuss both approaches. First, we will discuss creating the DropDownList using the hard-coded value then we will see how to create the DropDownList with the values coming from a database.
The HtmlHelper class in MVC provides two extension methods to generate the <select> element in a view. They are
- DropDownList() Helper Method
- DropDownListFor() Helper Method
DropDownList() HTML Helper Method in ASP.NET MVC:
There are 8 overloaded versions available for the DropDownList HTML Helper method as shown in the below image.
Parameters:
- htmlHelper: The HTML helper instance that this method extends.
- name: The name of the form field to return.
- selectList: A collection of System.Web.Mvc.SelectListItem objects that are used to populate the drop-down list.
- optionLabel: The text for a default empty item. This parameter can be null.
- htmlAttributes: An object that contains the HTML attributes to set for the element.
Returns: Returns a single-selection select element using the specified HTML helper, the name of the form field, the specified list items, an option label, and the specified HTML attributes.
Example: DropDownList HTML Helper Method in ASP.NET MVC Application
The following code will generate a department dropdown list. The first item in the drop-down list will be “Select Departmentâ€.
The downside of hard-coding the DropDownList value within the code itself is that if we have to add or remove departments from the DropDownList then the code needs to be modified each and every time.
How to set the Dropdown list values from the database in the ASP.NET MVC Application?
Most of the time or in real-time applications, we generally get the data from a database. To understand this lets create a Department Class and then populate the values within the controller as shown below. Add Department.cs class file in Models folder as shown below.
To pass the list of Departments from the controller store them in a “ViewBag†as shown below
Or you can also do the same thing in the following way
Now in the “Index†view access the Department list from “ViewBag†as shown below
@Html.DropDownList(“Departmentsâ€, @ViewBag.Departments as List<SelectListItem>, “Select Departmentâ€,new { @class = “form-controlâ€})
If you inspect the dropdown list then it will generate the below code
In the above example, the first parameter is the property name for which we want to display the list of items. The second parameter is the list of values which are going to be displayed within the DropDownList. Here we have used the ViewBag mechanism to get the department values. The third parameter is the label which is nothing but the first item in the drop-down list and the fourth parameter is for the Html attributes like CSS to be applied on the dropdown list.
How to use Enum to set the Dropdown list values in ASP.NET MVC Application?
Lets see how to use Enum to set the Dropdown list values. In this example, we are going to set the Gender Values from the enum.
In Department.cs file add the following enum
Copy and paste the following code in the index view
When we run the application, it will generate the following HTML
In the above example, the first parameter is the property name for which we want to display the list items. The second parameter is the list of values which is going to be displayed in the drop-down list. We have used Enum methods to get Gender enum values. The third parameter is the label which will be the first list item in the drop-down list and the fourth parameter is for the Html attributes like CSS to be applied on the dropdown list.
Please note that you can add @using HTML_HELPER.Models namespace into the <namespaces> section in web.config which is present within the Views folder instead of using @using to include the namespace in all the views.
DropDownListFor HTML Helper in ASP.NET MVC Application:
The DropDownListFor() HTML Helper method is a strongly typed extension method. This helper method is used to generate an <select> element for the property which is needed to be specified by using a lambda expression. The DropDownListFor() HTML Helper method will bind a specified property of a model object to the drop-down list control. As a result, it automatically displays the list items in the drop-down list. There are 6 overloaded versions available for the DropDownListFor HTML Helper method in ASP.NET MVC Framework as shown in the below image.
Parameters:
- htmlHelper: The HTML helper instance that this method extends.
- expression: An expression that identifies the object that contains the properties to display.
- selectList: A collection of System.Web.Mvc.SelectListItem objects that are used to populate the drop-down list.
- optionLabel: The text for a default empty item. This parameter can be null.
- htmlAttributes: An object that contains the HTML attributes to set for the element.
Type parameters:
- TModel: The type of model.
- TProperty: The type of value.
Returns: Returns an HTML select element for each property in the object that is represented by the specified expression using the specified list items, option label, and HTML attributes.
Example: DropDownListFor HTML Helper Method in ASP.NET MVC Application
We are going to use the following models to understand the DropDownListFor HTML Helper.
Employee.cs
Department.cs
Modify the EmployeeController as shown below.
Modify the Index.cshtml file as shown below
Or if you want to show the selected Department of that particular employee then copy and paste the following code
In the above example, the first parameter in DropDownListFor() HTML Helper method is a lambda expression that specifies the model property to be bind with the select element. We have specified Gender property of enum type and DepartmentID property. The second parameter specifies the items to show into the dropdown list using SelectList. The third parameter is the option Label which will be the first item of the drop-down list.