0

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?

Community
  • 1
  • 1
Marko
  • 12,543
  • 10
  • 48
  • 58
  • I would recommend leaving it the way it is! Using the "string" values from your Enum is the best recommended practice. Plus, MVC will automatically "map" that value correctly back to your model. – Scott Rippey Dec 15 '11 at 00:03
  • 1
    Also, "Enum" is short for "Enumeration", not "Enumerator" ... in .NET, the difference is significant, so you might want to re-word your question. – Scott Rippey Dec 15 '11 at 00:05
  • Point taken Scott and title has been fixed... Thanks! – Marko Feb 26 '13 at 04:08

1 Answers1

3

I think you need to set the enums to a number like so:

Public Enum UserSearchFields
    LastName = 0
    FirstName = 1
    Email = 2
    UniqueID = 3
End Enum

Even if this is an unecessary step, you need to cast it to an int when you write it out like so:

<select id="search_type" style="width: 100%;">
    <option value="@((int)SOLEPortal.UserSearchFields.LastName)" selected="selected">Last Name</option>
    <option value="@(((int)SOLEPortal.UserSearchFields.FirstName)">First Name</option>
    <option value="@((int)SOLEPortal.UserSearchFields.Email)">E-mail</option>
    <option value="@((int)SOLEPortal.UserSearchFields.UniqueID)">Unique ID</option>
</select>

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.

mccow002
  • 6,754
  • 3
  • 26
  • 36
  • Setting explicit `Enum` values is unnecessary. This will result in the exact same enum values as the original code. – Scott Rippey Dec 14 '11 at 23:58
  • Scott, mccow002 answer's includes a cast to int which what Marko needs to do. – rino.batin Dec 15 '11 at 01:52
  • I knew the cast was needed, but I wasn't sure about setting the enum values explicitly. So Marko, you don't have to do that :) – mccow002 Dec 15 '11 at 05:20
  • Thanks for the answer I sort of knew that I can cast these values but I'm baffled as to why... The underlying type of the enumerators is integer and everywhere else I use enumerators, for example in controller logic they are working off of the underlying type. As a matter of fact if you need to get the string value you have to specifically uset ToString() method. Why is it that the view renders then automatically as string is what I'm trying to understand... – Marko Dec 15 '11 at 13:47
  • 2
    The view wants everything as a string, so it makes sense it automatically call ToString. Every object in .NET has a ToString() method - you're garanteed to get SOMETHING. However, casting as an int will result in a cast exception the majority of the time. The view engine probably just sees everything as an object to avoid casting exceptions - therefore you get ToString() instead of (int). – mccow002 Dec 15 '11 at 19:34