MVC EnumDropDownListFor with Enum Display Description属性作为值

时间:2021-06-16 23:10:50

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).

现在我想将此枚举绑定到DropDownlist,在下拉项目显示文本和描述中显示枚举值(绿色,蓝色)作为项目值(bg-green,bg-blue)。

When I bind the Dropdown with EnumDropDownListFor helper method

当我使用EnumDropDownListFor帮助方法绑定Dropdown时

@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.

它将项值设置为枚举值(0,1),并且找不到将值设置为显示描述的方法。

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

如何将值设置为Enum Display Description属性?

1 个解决方案

#1


7  

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

您需要从Enum获取显示名称(DisplayAttribute),在下面的示例中设置Enum Display Description属性的值

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

Helper方法GetDescriptionOfEnum通过枚举名称获取Description属性

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,
    }

#1


7  

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

您需要从Enum获取显示名称(DisplayAttribute),在下面的示例中设置Enum Display Description属性的值

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

Helper方法GetDescriptionOfEnum通过枚举名称获取Description属性

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,
    }