Enum DropDownList in ASP.NET MVC

I created an HTML Helper for rendering a dropdownlist aka select element from the name/value pairs in an enum and thought I would share.

It's fairly simple to use: add the source code file to your project and recompile. You should then have two additional Html.Helper overridden methods for DropDownList.

Usage:

Html.DropDownList(string Name, Type enumtype)

Html.DropDownList(string Name, Type enumtype, object selectedEnum)

Examples of usage:

<%=Html.DropDownList("Status", typeof(StatusEnum), Model.Status) %>

Finally, the source, also available for download. Note class uses extension methods and is static with static methods.

using System.Collections.Generic;

namespace System.Web.Mvc
{
    public static class EnumDropDownList
    {
        /// <summary>
        /// Creates a dropdown list from an enum
        /// </summary>        
        /// <param name="name">name of the element</param>
        /// <param name="type">the enum type</param>
        /// <returns></returns>
        public static string DropDownList(this HtmlHelper helper, string name, Type type)
        {
            return DropDownList(helper, name, type, null);
        }

        /// <summary>
        /// Creates a dropdown list from an enum.
        /// </summary>        
        /// <param name="name">name of an element</param>
        /// <param name="type">the enum type</param>
        /// <param name="selected">selected enum value</param>
        /// <returns></returns>
        public static string DropDownList(this HtmlHelper helper, string name, Type type, object selected)
        {
            if (!type.IsEnum)
                throw new ArgumentException("Type is not an enum.");

            if(selected != null && selected.GetType() != type)
                throw new ArgumentException("Selected object is not " + type.ToString());
           
            var enums = new List<SelectListItem>();
            foreach (int value in Enum.GetValues(type))
            {
                var item = new SelectListItem();
                item.Value = value.ToString();
                item.Text = Enum.GetName(type, value);

                if(selected != null)
                    item.Selected = (int)selected == value;

                enums.Add(item);
             }

            return System.Web.Mvc.Html.SelectExtensions.DropDownList(helper, name, enums);
        }
    }
}

AttachmentSize
EnumDropDownList.cs1.75 KB

Comments

#1 Anonymous

I ran into a bit of a funny issue with this. Ended up needing to set the Text AND Value of the select list items to the value you are currently setting for text.

See this StackOverflow post for more info: http://stackoverflow.com/questions/3032743/selected-item-in-dropdown-lis...

#2 khuang

That's quite an interesting bit of detail there. Sadly I'm not the one using MVC (that's Brian's territory) so I can't confirm but I think the link to the StackOverflow issue is more than sufficient for the time being.

Recent comments