ASP.NET MVC 5.1 which is an update to the version 5.0 of MVC includes some new useful
features.One such feature is Enum support in Views.
In the previous versions of MVC we have to use some some workarounds when displaying Enum values in the Views.For displaying enum values in the dropdown in previous versions please refer the following Populate dropdown with Enum in MVC
In MVC 5.1 we can directly display the enum values in the views using the EnumDropDownListFor helper method.
We use this method as
@Html.EnumDropDownListFor(model=> model.EnumProperty)
model.EnumProperty above refers to a property of Enum type.
EnumDropDownListFor is a strongly typed helper method so we need to pass a lambda expression to this method.In the lambda expression we need to specify the enum which we want to display in the dropdown.
In the following example we are defining Employee class.Employee contains a property of ProgrammingLanguages which is an enum type.
public class Employee { public string Name { get; set; } public int EmployeeId { get; set; } public string Department { get; set; } public ProgrammingLanguages Language { get; set; } }
ProgrammingLanguages enum is defined as:
public enum ProgrammingLanguages { [Display(Name = "C#")] CSharp, [Display(Name = "VB.NET")] VB, [Display(Name = "Java")] Java, [Display(Name = "SQL")] SQL }
In the ProgrammingLanguages enum we have specified the Display attribute for the enum values.The dropdown rendered will automatically display the value we specified in the Name property of Display attribute.This saves us workaround for displaying different value as the text as in the previous versions.
Now we can create and initialize Employee object in the action method as:
public ActionResult Index() { Employee emp = new Employee(); emp.Name = "Mike"; emp.EmployeeId = 01; emp.Department = "Application Development"; emp.Language = Programminglanguages.Java; return View(emp); }
The important thing to note is how we have specified the Language property above.Now when the dropdown is rendered the property assigned to the Language property, which is Programminglanguages.Java , will be automatically selected in the dropdown.
We specify the markup for the Employee in the view as
@model Models.Employee <table> <tr> <td> @Html.LabelFor(x => x.Name) </td> <td> @Html.DisplayFor(x => x.Name) </td> </tr> <tr> <td> @Html.LabelFor(x => x.EmployeeId) </td> <td> @Html.DisplayFor(x => x.EmployeeId) </td> </tr> <tr> <td> @Html.LabelFor(x => x.Department) </td> <td> @Html.DisplayFor(x => x.Department) </td> </tr> <tr> <td> @Html.LabelFor(x => x.Language) </td> <td> @Html.EnumDropDownListFor(x => x.Language) </td> </tr> </table>
When the dropdown is rendered by default “Java” will be selected since we have specified the following for the Language property:
emp.Language = Programminglanguages.Java;
So as we have seen now we can easily populate the dropdown using EnumDropDownListFor helper method in MVC 5.1 method unlike the previous versions where we had to use some workarounds.Also we can easily set the selected value in the dropdown as above.
zohaib says
can we use required attribute of html5 with html.enumdroplistfor?
ashish says
Yes you can use it like:
@Html.EnumDropDownListFor(x => x.vals,”-“,new {required=”required”})