CodeDigest.Com Logo
Featured:

Bind Enum to DropDownList Control in Asp.Net MVC

Tagged as: Asp.Net MVC Posted By

My previous article Different Ways of Binding DropDownList in Asp.Net MVC demonstrated different ways of binding DropDownList control by fetching data from database. In this article, let us learn to populate DropDownList control using enumeration defined in our project. Enum or Enumeration is a data type which is used to define named constants. Since enum members have string names and an integer value associated with it it can be used to populate DropDownList control.

For easy understanding, let’s use the below Department enum object to bind DropDownList control.

 

public enum Departments

{

     IT = 1,

     CSE = 2,

     MECH = 3

}

 

Using Html.EnumDropDownListFor

Asp.Net MVC 5.1, we have a new helper method called @Html.EnumDropDownListFor for binding enum object to DropDownList control. The below view

View

 

@Html.EnumDropDownListFor(model => model.DepartmentId,"Select")

 

Model class

public class Employee
{
    //Table properties
    [Key]
    public int EmployeeId { get; set; }
    public Departments DepartmentId { get; set; }
    
    //Removed for brevity
}
 

The above code will use the enum member name IT, CSE, MECH as Text property and the integer values as Value when binding the control. Refer below.

This helper methods also support Data Annotation Display attribute to display the DropDownList text property. So, adding a display attribute will make the above helper method to use the display name specified in the Display attribute as mentioned below.

 

public enum Departments
{
    [Display(Name ="Information Technology")]
    IT = 1,
    [Display(Name = "Computer Science")]
    CSE = 2,
    [Display(Name = "Mechanical")]
    MECH = 3
}
 

When executed, you will see output like below.

Using EnumHelper Class

There is a new enum helper class called EnumHelper found under System.Web.Mvc.Html namespace released with Asp.Net 5.0 which can be used to populate enum object to DropDownList control.

 

@Html.DropDownListFor(model => model.DepartmentId,EnumHelper.GetSelectList(typeof(Departments)), "Select")

 

 

Binding Enum to DropDownList in Lower version of Asp.Net MVC

The above approaches work only Asp.Net MVC version 5.0 and above. For binding enum object in Asp.Net MVC version older than 5.0, we can use the below code.

Action

public ActionResult edit(int id)
{
    Employee emp = db.Employees.Where(e => e.EmployeeId == id).FirstOrDefault();
    IEnumerable<SelectListItem> values = from Departments d in Enum.GetValues(typeof(Departments))
                                         select new SelectListItem
                                         {
                                             Text = d.ToString(),
                                             Value = Convert.ToInt32(d).ToString()
                                         };
    ViewBag.DepartmentId = new SelectList(values, "Value", "Text", emp.DepartmentId);
    return View(emp);
}

 

View

@Html.DropDownList("DepartmentId","Select")

 

 



Feedback

Comments

it only records enum numbers
it only records enum numbers, how can I fix it? Code: @Html.DropDownListFor(model => model.Unvan2, EnumHelper.GetSelectList(typeof(DSI_Web_Application_2.Enums.Unvan)), "Ünvan 2 Seçiniz...", new { @class = "form-control" })
Commented by Davut SATICI on 4/8/2020 6:06:05 AM

gracias
funciono muy bien
Commented by DRequeno on 3/15/2018 2:12:06 PM