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?

3
  • 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. Commented Dec 15, 2011 at 0: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. Commented Dec 15, 2011 at 0:05
  • Point taken Scott and title has been fixed... Thanks! Commented Feb 26, 2013 at 4:08

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Setting explicit Enum values is unnecessary. This will result in the exact same enum values as the original code.
Scott, mccow002 answer's includes a cast to int which what Marko needs to do.
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 :)
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...
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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.