Using EnumDropDownListFor HTML Helper

This is to explain EnumDropDownListFor HTML helper behaviour in ASP.NET MVC 5 with example code. Please remember, EnumDropDownListFor is not available in earlier version MVC, i.e. ASP.NET MVC 4 or before.

Default EnumDropDownListFor Behaviour

First, let’s say you want to create a drop down list from below employment type enum, then in your model you can create public non-nullable property of the enum type, see below code:

Then in EmployeeController Create action:

Now in your view inside html form, you can simply use EnumDropDownListFor HTML helper like this:

The HTML helper EnumDropDownListFor will generate drop down list with the following items:

  • ToBeDetermined (value = 0, selected)
  • FullTime (value = 1)
  • PartTime (value = 2)

Specify Default Selected Item

By default, if no selected item specified, enum item of value 0 will be selected. But for very common reason if you need to set a default value for the drop down list, you can create a new view model object in the controller action first, then set inital value for the property of type enum and pass the newly created model object to the razor view. For example in EmployeeController Create action:

This time EnumDropDownListFor will generate drop down list with the following items:

  • ToBeDetermined (value = 0)
  • FullTime (value = 1, selected)
  • PartTime (value = 2)

Enum Does Not Have Item of Value 0

What happens if you explicitly specify enum item values that start from 1 like this:

By default, if there enum member of value 0 does not exist, EnumDropDownListFor will automatically create a HTML drop down list with a prompt, i.e. a empty text select list item with item value of 0.

So, EnumDropDownListFor will generate drop down list with the following items:

  • [Empty Text] (value = 0, selected)
  • FullTime (value = 1)
  • PartTime (value = 2)

But if you specify default value for the property such as in Create action example above, a prompt item will not be generated, and you will have drop down list with only the following items:

  • FullTime (value = 1, selected)
  • PartTime (value = 2)

3 thoughts on “Using EnumDropDownListFor HTML Helper”

  1. CEVDET says:

    thank you very Much broo:)

  2. andy says:

    how do you get the selected value of ENUM , on POST inside your controller?

  3. Fatih ÖZTÜRK says:

    oh! thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.