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 8636717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:17:40+00:00 2026-06-12T10:17:40+00:00

I’ve got a nullable enum that, unlike others on the same page, doesn’t work.

  • 0

I’ve got a nullable enum that, unlike others on the same page, doesn’t work. I have an enum, Title whereby using the extension method will help to populate a drop-down list on the page. Here’s what the ViewBag declaration looks like:

ViewBag.TitleList = EnumExtensions.ToSelectList<Title>("[select]");

Now, perhaps someone could explain it to me, but this is where the black magic happens when it comes to binding in MVC. If the page is invalid when calling if(ModelState.IsValid) then upon re-rendering the screen, the above statement is called again. However this time, the correct drop-down item will be selected (dependent on which one you had selected at the time).

Digging deeper, this is the method declarations:

    public static SelectList ToSelectList<TEnum>(string nullEntry = null) where TEnum : struct
    {
        return ToSelectList<TEnum>(nullEntry, null);
    }


    public static SelectList ToSelectList<TEnum>(string nullEntry = null, string selectedValue = null) where TEnum : struct
    {
        var enumType = typeof(TEnum);
        var values = Enum.GetValues(enumType).OfType<TEnum>();
        List<SelectListItem> items =  ToSelectList<TEnum>(values, nullEntry, selectedValue);
        SelectList sl = new SelectList(items, "Value", "Text", selectedValue);
        return sl;
    }

    public static List<SelectListItem> ToSelectList<T>(this IEnumerable<T> enumerable, string nullEntry, string selectedValue = null)
    {
        List<SelectListItem> items;

        if ((typeof(T).IsEnum))
        {
            items = enumerable.Select(f => new SelectListItem()
            {
                Text = f.GetDescription(),
                Value = f.ToString(),
                Selected = f.ToString() == selectedValue
            }).ToList();
        }
        else
        {
            items = enumerable.Select(f => new SelectListItem()
            {
                Text = f.ToString(),
                Value = f.ToString()
            }).ToList();
        }

        if (!string.IsNullOrEmpty(nullEntry))
        {
            items.Insert(0, new SelectListItem() { Text = nullEntry, Value = "" });
        }

        return items;
    }

There’s just some overloads to handle random cases, although presumably some of these won’t be needed.

As I say, the correct item will be selected for other enumerations, but for this particular one, Title, it will not. Here’s the enum declaration:

public enum Title
{
    Mr,
    Miss,
    Mrs,
    Ms
}

And finally, the declaration using DropDownListFor on the page itself;

@Html.DropDownListFor(x => x.Title, (SelectList)ViewBag.TitleList)

The problem is that when I first visit the page, the selected item is always “[select]” (when the provided enum value is null in the model). However, the model property Title definitely has a value set, and the SelectedItem property is set for the drop-down list too, but on screen, it defaults to “[select]” which is unexpected.

Any ideas?

  • 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-12T10:17:40+00:00Added an answer on June 12, 2026 at 10:17 am

    Could it be because of the name Title? Try changing it to another name just to see.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and

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.