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?
An enumeration is a great way to define a set of constant values in a single data type. If you want to display an enum's element name on your UI directly by calling its ToString() method, it will be displayed as it has been defined.
And to get the display name anywhere in a C# project, you can add an enum extension class that returns the display name property. If the property is missing (like it is for the first two enum values above), it falls back to the string equivalent.
Create an enum, ActionType, under the Models folder as in the following code snippet. I populate the DropDownList using a SelectListItem type list. So we need to first create a list from enum and thereafter that list binds with the DropDownListFor method of the HTML Helper class on the view.
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,
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With