繁体   English   中英

获取枚举的描述并将其用作MVC中dropdownlist的text属性?

[英]Getting description of Enum and using this as text property of dropdownlist in MVC?

我的视图内有一个下拉控件。 现在我希望它具有枚举的描述作为文本。 我最不担心ID,但我需要文本格式正确。

请参考以下代码。

public static List<EnumModel> GetEnumList<T>()
            {
                var enumValues = Enum.GetValues(typeof(T)).Cast<T>().Select(rentalType => new EnumModel()
                {
                    Value = Convert.ToInt32(rentalType),
                    Name = GetDisplayName<T>(rentalType, false)
                }).ToList();

                return enumValues;
            }

            public static string GetDisplayName<T>(T value, bool isDisplayName)
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value", "Enum value Empty");
                }

                var type = value.GetType();
                var field = type.GetField(value.ToString());
                if (field == null)
                {
                    return value.ToString();
                }

                var attributes = ((DisplayAttribute[])field.GetCustomAttributes(typeof(DisplayAttribute), false)).FirstOrDefault();
                return attributes != null ? isDisplayName == true ? attributes.GetDescription() : attributes.Description : value.ToString();
            }

     public class EnumModel
            {
                /// <summary>
                /// Gets or sets the value
                /// </summary>
                public int Value { get; set; }

                /// <summary>
                /// Gets or sets the name
                /// </summary>
                public string Name { get; set; }
            }

您可以将List<EnumModel>作为带有名称和值的ENUM列表获取。您需要做的就是从List<EnumModel>创建一个List<SelectListitem> List<EnumModel>

希望这可以帮助。

使用此代码并绑定您的下拉菜单-

public static List<SelectListItem> GetSelectList(Type enumType, String SelectedValue, Boolean IsValString = true)
            {
            Array values = Enum.GetValues(enumType);
            List<SelectListItem> selectListItems = new List<SelectListItem>(values.Length);

            foreach (var i in Enum.GetValues(enumType))
            {
                String name = Enum.GetName(enumType, i);
                String desc = name;
                FieldInfo fi = enumType.GetField(name);
                var attributes = fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
                String result = attributes.Length == 0 ? desc : ((DescriptionAttribute)attributes[0]).Description;
                var selectItem = new SelectListItem()
                {
                    Text = result,
                    Value = (IsValString) ? i.ToString() : ((Int32)i).ToString()
                };

                if ((SelectedValue != null) && (SelectedValue.Equals(selectItem.Value)))
                {
                    selectItem.Selected = true;
                }

                selectListItems.Add(selectItem);
            }
            return selectListItems;
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM