I have the following enumerator in my code:
Public Enum UserSearchFields
LastName
FirstName
Email
UniqueID
End Enum
I try to populate a drop down list in the view with the values from this enumerator:
<select id="search_type">
<option value="@UserSearchFields.LastName" selected="selected">Last Name</option>
<option value="@UserSearchFields.FirstName">First Name</option>
<option value="@UserSearchFields.Email">E-mail</option>
<option value="@UserSearchFields.UniqueID">Unique ID</option>
</select>
But for some reason when page is rendered the value field contains the string representations of the enumerator not the underlying integer values. For example the option value field will be “LastName” instead of “0”… Why is this the case and am I making some sort of mistake?
P.S. I’m aware that I can populate a drop down list from an Enumerator such as How do you create a dropdownlist from an enum in ASP.NET MVC? but i just would like to know why is this issue happening?
I think you need to set the enums to a number like so:
Even if this is an unecessary step, you need to cast it to an int when you write it out like so:
A better way to do this would be to create an extension method that automatically writes out a drop down list from an enum, but this is a good starting place.