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

  • Home
  • SEARCH
  • 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 6945019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:22:55+00:00 2026-05-27T13:22:55+00:00

I know there is several threads on this topic. I see a lot of

  • 0

I know there is several threads on this topic. I see a lot of solutions but most of them demands the knowledge of which type the enum is. Since Enum by default renders uses the template for string, it’s more difficult to find out the exact type.

Has someone got a complete example on how to solve this?

Edit. Based on Roys suggestion, here’s my code.

HtmlHelpers.cs

public static MvcHtmlString EnumDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) where TModel : class
{
    var type = htmlHelper.ViewData.ModelMetadata.ModelType;
    var inputName = type.Name;
    var value = htmlHelper.ViewData.Model == null ? default(TProperty) : expression.Compile()(htmlHelper.ViewData.Model);
    var selected = value == null ? String.Empty : value.ToString();
    return htmlHelper.DropDownList(inputName, ToSelectList(type, selected));
}

private static IEnumerable<SelectListItem> ToSelectList(Type enumType, string selectedItem)
{
    var items = new List<SelectListItem>();
    foreach (var item in Enum.GetValues(enumType))
    {
        var fi = enumType.GetField(item.ToString());
        var attribute = fi.GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault();
        var title = attribute == null ? item.ToString() : ((DescriptionAttribute)attribute).Description;
        var listItem = new SelectListItem
        {
            Value = ((int)item).ToString(),
            Text = title,
            Selected = selectedItem == ((int)item).ToString()
        };
        items.Add(listItem);
    }

    return new SelectList(items, "Value", "Text");
}

Viewmodel

public class AddTestForm
{
    [UIHint("Enum")]
    public EnumType Type { get; set; }

    public string Description { get; set; }

public enum EnumType
{
    One = 1,
    Two = 2
}

EditorTemplates/Enum.cshtml

@model object

@Html.EnumDropDownListFor(x => x)

And in views…

@Html.EditorForModel()

The problem now is that I can’t get the enum to fill after the form has been posted.

  • 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-05-27T13:22:55+00:00Added an answer on May 27, 2026 at 1:22 pm

    Your helper generates wrong names to the generated select elements:

    <select id="Type_EnumType" name="Type.EnumType">
    

    The DefaultModelBinder uses the name attribute to match the property names. Your helper generated name "Type.EnumType" doesn’t match with the Model property name "Type". To make it work instead of the Type name you need to get the property name from the expression. It’s quite easy with the ExpressionHelper class:

    public static MvcHtmlString EnumDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) where TModel : class
    {
        ...
        return htmlHelper.DropDownList(ExpressionHelper.GetExpressionText(expression), ToSelectList(type, selected));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there are several threads on this already, but I've hit a brick
I know there are several threads and posts regarding this issue in the internet
I know there have been a few threads on this before, but I have
I know that there are several threads posted about this issue, however I believe
I know there are several plugins that do asynchronous processing. Which one is the
I know there are several libraries to connect to XMPP servers, but is there
I know there are a lot of positive things mod-rewrite accomplishes. But are there
I know there are a lot of regex threads out there by I need
I know there are several ways to deploy a .net windows client application: There's
I know it is easy to recommend several cross platform libraries. However, are there

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.