Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7959759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:37:22+00:00 2026-06-04T04:37:22+00:00

I have an extension method that I’m trying to use where you can use

  • 0

I have an extension method that I’m trying to use where you can use an enum property to create a dropdown list, and set the selected item:

public enum DefaultEnumSelectItemOptions
{
    AddDefaultItemIfEnumIsZero,
    ZeroEnumIsDefaultItem
}
public static SelectList ToSelectList(this object enumObj, DefaultEnumSelectItemOptions option = DefaultEnumSelectItemOptions.AddDefaultItemIfEnumIsZero)
{
    var asEnum = Enum.Parse(enumObj.GetType(), enumObj.ToString());
    var values = Enum.GetValues(enumObj.GetType());
    var dataItems = new List<Tuple<string, int>>();
    dataItems.Add(new Tuple<string, int>("Select One", -1));
    for (int i = 0; i < values.Length; i++)
    {
        int enumValue = (int)values.GetValue(i);
        if (enumValue == 0)
        {
            if (option != DefaultEnumSelectItemOptions.AddDefaultItemIfEnumIsZero)
            {
                dataItems.Add(new Tuple<string, int>(values.GetValue(i).ToString(), enumValue));
            }
        }
        else
        {
            dataItems.Add(new Tuple<string, int>(values.GetValue(i).ToString(), enumValue));
        }
    }
    var selectedItemValue = (int)enumObj;
    if (selectedItemValue == 0 && option == DefaultEnumSelectItemOptions.AddDefaultItemIfEnumIsZero)
    {
        selectedItemValue = -1;
    }
    return new SelectList(dataItems, "Item2", "Item1", selectedItemValue);
}

A model looks like this:

public enum PropertyTypes
{
    Unknown=0,
    Vehicle,
    Other
}

[DataContract]
public class Property : ClaimEntity
{
    [DataMember]
    public PropertyTypes PropertyType { get; set; }

    public Property()
    {
        this.PropertyType = PropertyTypes.Vehicle;
    }
}

Finally the view looks like this:

@Html.DropDownListFor(m => m.PropertyType, Model.PropertyType.ToSelectList())

When I set a breakpoint in the extension method, it appears to be correct, but the selected option is not appearing in the html.

dropdown issue

What am I doing wrong?

edit I changed it to use the SelectListItem as suggested, however I still am not seeing the value selecting:

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:37:23+00:00Added an answer on June 4, 2026 at 4:37 am

    I’m actually running into this exact problem myself and discovered that the DropDownListFor (and DropDownList) helpers are a little too smart for their own good.

    Even if you pass in a set of SelectListItem with one of them Selected = true, the helper will actually evaluate your model, do a Convert.ToString() on it, and attempt to match the value. If it doesn’t find the value it will select nothing.

    Personally I believe this is a major mistake in MVC, one that they do not appear to be correcting in MVC4. It is a completely bogus assumption on their part that that the ToString() method of an object would match the value (rather than the display text) in a dropdown.

    edit: as for ways to fix this…

    1. Change your ToString() method to return the value and figure out a different way to get your display text.
    2. Since you’re using an enum, you could just the string version for both your value and your display text. It will still bind fine.
    3. Build your own dropdown
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write an extension method that I can use to copy values
I am trying to create an extension method that i can forward a IList
I have an extension method on HttpRequest that I'm trying to use inline on
I have an extension method which I can use from the .cs codebehind of
I have a list that contains FrameworkElements and I want to create an extension
I have an extension method that I wrote but when I use it on
I have an extension method that I would like to overload so it can
I have the following extension method that takes a List and converts it to
I would like to create a safe sum extension method that would have the
I have a layout with an extension method that takes a list of menu

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.