7

I have an Enum with Display Description attribute,

public enum CSSColours
    {
        [Display(Description = "bg-green")]
        Green,

        [Display(Description = "bg-blue")]
        Blue,
    }

Now I want to bind this Enum to a DropDownlist, showing the Enum value (Green, Blue) in the Dropdown item display text and Description as the item Value (bg-green, bg-blue).

When I bind the Dropdown with EnumDropDownListFor helper method

@Html.EnumDropDownListFor(c => dm.BgColor)

It sets the item value to Enum value (0, 1), and couldn't find a way to set the value to Display Description.

How can I set the value to Enum Display Description attribute?

Nalaka526
  • 11,278
  • 21
  • 82
  • 116
  • it's discussed http://stackoverflow.com/questions/13099834/how-to-get-the-display-name-attribute-of-an-enum-member-via-mvc-razor-code – cutit Jul 20 '15 at 04:36

1 Answers1

8

You need to get display name (DisplayAttribute) from Enum, Check below Example to set the value of Enum Display Description attribute

Action (binding dropdownlist)

public ActionResult Index()
        {   
            var enumDataColours = from CSSColours e in Enum.GetValues(typeof(CSSColours))
                           select new
                           {
                               ID = StaticHelper.GetDescriptionOfEnum((CSSColours)e),
                               Name = e.ToString()
                           };
            ViewBag.EnumColoursList = new SelectList(enumDataColours, "ID", "Name");
            return View();
        }

Helper method GetDescriptionOfEnum to get Description attribute by enum name

public static class StaticHelper
    {
        public static string GetDescriptionOfEnum(Enum value)
        {
            var type = value.GetType();
            if (!type.IsEnum) throw new ArgumentException(String.Format("Type '{0}' is not Enum", type));

            var members = type.GetMember(value.ToString());
            if (members.Length == 0) throw new ArgumentException(String.Format("Member '{0}' not found in type '{1}'", value, type.Name));

            var member = members[0];
            var attributes = member.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute), false);
            if (attributes.Length == 0) throw new ArgumentException(String.Format("'{0}.{1}' doesn't have DisplayAttribute", type.Name, value));

            var attribute = (System.ComponentModel.DataAnnotations.DisplayAttribute)attributes[0];
            return attribute.Description;
        }
    }

Razor view

@Html.DropDownList("EnumDropDownColours", ViewBag.EnumColoursList as SelectList)

Enum

public enum CSSColours
    {
        [Display(Description = "bg-green")]
        Green,

        [Display(Description = "bg-blue")]
        Blue,
    }
Abbas Galiyakotwala
  • 2,949
  • 4
  • 19
  • 34
  • The only thing I would change in this answer is to change the DropDownList to a DropDownListFor like this: '@Html.DropDownListFor(m => m.CSSColour, ViewBag.CSSColours as SelectList, "Select One")' – Gwasshoppa Dec 06 '16 at 21:35