Click here to Skip to main content
13,447,776 members (53,481 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 6 Apr 2011

HtmlHelper for enums in ASP.NET MVC3

, 6 Apr 2011
Rate this:
Please Sign up or sign in to vote.
How to get enums to work with HtmlHelpers.

Have you tried to get enums working with HtmlHelpers? It can be a hassle and here is my solution for it. It requires that you tag each entry in the enum with a [Description] attribute.

public static class SelectExtensions
    public static string GetInputName<TModel, TProperty>(
           Expression<Func<TModel, TProperty>> expression)
        if (expression.Body.NodeType == ExpressionType.Call)
            MethodCallExpression methodCallExpression = 
            string name = GetInputName(methodCallExpression);
            return name.Substring(expression.Parameters[0].Name.Length + 1);

        return expression.Body.ToString().Substring(
               expression.Parameters[0].Name.Length + 1);

    private static string GetInputName(MethodCallExpression expression)
        // p => p.Foo.Bar().Baz.ToString() => p.Foo OR throw...
        MethodCallExpression methodCallExpression = 
                  expression.Object as MethodCallExpression;
        if (methodCallExpression != null)
            return GetInputName(methodCallExpression);
        return expression.Object.ToString();

    public static MvcHtmlString EnumDropDownListFor<TModel, TProperty>(
           this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, 
           TProperty>> expression) where TModel : class
        string inputName = GetInputName(expression);
        var value = htmlHelper.ViewData.Model == null
            ? default(TProperty)
            : expression.Compile()(htmlHelper.ViewData.Model);

        return htmlHelper.DropDownList(inputName, 
               ToSelectList(typeof(TProperty), value.ToString()));

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

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


@Html.EnumDropDownListFor(m => m.SubscriptionMode);

If you want to be able to use it for validation, you need to be able to have an “undefined” entry in your enum and do the following change:

// replace "Value = ((int)item).ToString()," in to select list.
Value = (int)item == 0 ? string.Empty : ((int)item).ToString(),


This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


About the Author

Founder 1TCompany AB
Sweden Sweden
Skip logfiles, try automated error handling!

I'm one of the founders of codeRR, a .NET service which takes care of everything related to exceptions, so that you can focus on writing code.

blog | twitter

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180318.3 | Last Updated 6 Apr 2011
Article Copyright 2011 by jgauffin
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid