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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:53:49+00:00 2026-06-17T23:53:49+00:00

The MVC HtmlHelper.DropDownFor method can be downright frustrating to use. More often than not

  • 0

The MVC HtmlHelper.DropDownFor method can be downright frustrating to use. More often than not your selection does not remain or your control is not bound correctly. How would you write a custom HTML helper to populate a dropdown from an enum?

  • 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-17T23:53:51+00:00Added an answer on June 17, 2026 at 11:53 pm

    I spent the last few hours trying to figure this one out, so might as well share what I found. After trying all sorts of permutations, creating a test app to try out various options and searching many articles, I got something that works for me.

    First point to mention. The SelectList class takes four parameters (last three are optional). If you don’t specify the selected value (last param), it will clear out any selected values you had set in your SelectListItem objects (assuming you created a list of those). This frustrated me for a while because I was setting one of the items Selected property to true, but once I create the SelectList object it was always set to false.

    Here’s the MVC source for SelectList for reference:

    public class SelectList : MultiSelectList
    {
        public SelectList(IEnumerable items)
            : this(items, null /* selectedValue */)
        {
        }
    
        public SelectList(IEnumerable items, object selectedValue)
            : this(items, null /* dataValuefield */, null /* dataTextField */, selectedValue)
        {
        }
    
        public SelectList(IEnumerable items, string dataValueField, string dataTextField)
            : this(items, dataValueField, dataTextField, null /* selectedValue */)
        {
        }
    
        public SelectList(IEnumerable items, string dataValueField, string dataTextField, object selectedValue)
            : base(items, dataValueField, dataTextField, ToEnumerable(selectedValue))
        {
            SelectedValue = selectedValue;
        }
    
        public object SelectedValue { get; private set; }
    
        private static IEnumerable ToEnumerable(object selectedValue)
        {
            return (selectedValue != null) ? new object[] { selectedValue } : null;
        }
    }
    

    Once I got past that little point I got my helper to correctly select the item from the list and correctly bind the value back. So here’s the helper method I created (The initial method was from another post, but that did nor work correctly for me):

    public static MvcHtmlString EnumDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes = null) where TProperty : struct, IConvertible
    {
        if (!typeof(TProperty).IsEnum)
            throw new ArgumentException("TProperty must be an enumerated type");
    
        var selectedValue = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).Model.ToString();
        var selectList = new SelectList(from value in EnumHelper.GetValues<TProperty>()
                                        select new SelectListItem
                                                    {
                                                        Text = value.ToDescriptionString(),
                                                        Value = value.ToString()
                                                    }, "Value", "Text", selectedValue);
    
        return htmlHelper.DropDownListFor(expression, selectList, htmlAttributes);
    }
    

    (The EnumHelper.GetValues and ToDescriptionString are my helper methods to return a list of enum values of a specified type and to get the EnumMember value property for the description for the enum) I can post that code if anyone wants it.

    The trick in that above code was telling SelectList what the value and text properties are as well as the selected value.

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

Sidebar

Related Questions

CS1928: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'DropDownList' and the best extension method
I use <%= Html.Action(ReadXML) %> and have this error: 'System.Web.Mvc.HtmlHelper' does not contain a
Exact error message: 'System.Web.Mvc.HtmlHelper<dynamic>' does not contain a definition for 'RouteUrl' and no extension
System.Web.Mvc has an HtmlHelper that contains a method called EditorFor that renders the editing
I want to use a MVC HtmlHelper similar to LabelFor. When using reflector on
I'm getting the following error: The type arguments for method 'System.Web.Mvc.Html.DisplayExtensions.DisplayFor<TModel,TValue>(System.Web.Mvc.HtmlHelper<TModel>, System.Linq.Expressions.Expression<System.Func<TModel,TValue>>, string)' cannot
I'm trying to create an Extension Method for MVC's htmlHelper. The purpose is to
I have created HtmlHelper in ASP.NET MVC 4 razor view engine C#. Can I
Does anybody know why could some HTML form controls be rendered using System.Web.Mvc.HtmlHelper (hidden,
I'm creating my first MVC helper method, but can't quite get it. I want

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.