If I would like to use my enums for dropdown lists in my ASP.NET MVC view so that I can have either the enum value or enum name as the select list item value and a more descriptive text as the select list item text how would I do that?
            Asked
            
        
        
            Active
            
        
            Viewed 6,643 times
        
    2
            
            
        - 
                    2See also http://stackoverflow.com/a/8825668/298053. Though we appreciate your initiative, make sure to search before posting a QA. With question/answer 1 minute apart, it was obvious you were going for just a reference--just make sure it's a new one. – Brad Christie Jan 26 '15 at 15:35
- 
                    If you want to promote that code, feel free to add an answer to linked question. The question is the same, the answers are too. – CodeCaster Jan 26 '15 at 15:35
- 
                    I see the other posts. I missed via my searches. My post its a lot cleaner without required a bunch of posts to be read and links to be followed. Adding to the other posts would just add to the problem. – Kirby Jan 26 '15 at 15:38
- 
                    For MVC 5 there's the `[Display]` attribute as mentioned in the duplicate, and [this answer from it](http://stackoverflow.com/a/16089319/266143) is basically the same as yours here - with less code and more illustration. I don't think adding a new Q&A helps people find results better. – CodeCaster Jan 26 '15 at 15:42
- 
                    Yes that is very cool about the [Display] attribute. Unfortunately we are on MVC 4 as of now. – Kirby Jan 26 '15 at 15:44
1 Answers
7
            Here is an example on how to do this:
public enum ExampleEnum
{
    [ComponentModel.Description("Example One")]
    ExampleOne,
    [ComponentModel.Description("Example Two")]
    ExampleTwo,
    [ComponentModel.Description("Example Three")]
    ExampleThree,
    [ComponentModel.Description("Example Four")]
    ExampleFour,
    ExampleWithNoDescription
}
@using Mvc4Scratch.Web.Helpers
@model Mvc4Scratch.Web.ViewModels.EnumExampleViewModel
@{
    ViewBag.Title = "EnumDropDownExample";
}
<h2>@Model.ExampleTitle</h2>
<div>
    @Html.LabelFor(model => model.ExampleEnum)
    @Html.EnumDropDownListFor(model => model.ExampleEnum)
</div>
using Mvc4Scratch.Web.Helpers;
namespace Mvc4Scratch.Web.ViewModels
{
    public class EnumExampleViewModel
    {
        public string ExampleTitle { get; set; }
        public ExampleEnum ExampleEnum { get; set; }
    }
}
using System;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web.Mvc;
using System.Web.Mvc.Html;
namespace Mvc4Scratch.Web.Helpers
{
    public static class Extensions
    {
        public static string GetName(this Enum value)
        {
            return Enum.GetName(value.GetType(), value);
        }
        public static string GetDescription(this Enum value)
        {
            var fieldInfo = value.GetType().GetField(value.GetName());
            var descriptionAttribute = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() as DescriptionAttribute;
            return descriptionAttribute == null ? value.GetName() : descriptionAttribute.Description;
        }
        public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> enumAccessor)
        {
            var propertyInfo = enumAccessor.ToPropertyInfo();
            var enumType = propertyInfo.PropertyType;
            var enumValues = Enum.GetValues(enumType).Cast<Enum>();
            var selectItems = enumValues.Select(s => new SelectListItem
                                                     {
                                                         Text = s.GetDescription(),
                                                         Value = s.ToString()
                                                     });
            return htmlHelper.DropDownListFor(enumAccessor, selectItems);
        }
        public static PropertyInfo ToPropertyInfo(this LambdaExpression expression)
        {
            var memberExpression = expression.Body as MemberExpression;
            return (memberExpression == null) ? null : memberExpression.Member as PropertyInfo;
        }
    }
}
 
    
    
        Kirby
        
- 1,739
- 1
- 17
- 21
- 
                    I think this is a very clean approach when you don't want to get a db table or larger class involved. It also show cases how useful extension methods can be too. – Kirby Jan 26 '15 at 15:16
- 
                    BTW check out C# Extension Methods course on Pluralsight by Elton Stoneman. Good stuff and you can see this in action! – Kirby Jan 26 '15 at 15:23
- 
                    It is indeed interesting source to read about extension methods. – Aryan Firouzian Jan 10 '19 at 08:52