从GetEnumSelectList <>获取文本值,而不是索引值

时间:2019-01-13 08:59:55

标签: c# asp.net-mvc asp.net-core

GetEnumSelectList <>返回一个索引值,而不是文本值。如何获得文本值?

Usage: 
<select asp-for="Policy" asp-items="Html.GetEnumSelectList(typeof(PolicyEnum))" class="form-control"></select>

Results:
<select class="form-control" id="Policy" name="Policy">
  <option value="0">SuperAdmin</option>
  <option value="1">Admin</option>
  <option value="2">User</option>
  <option value="3">ClientAdmin</option>
  <option value="4">ClientUser</option>
</select>

Desired Results:
<select class="form-control" id="Policy" name="Policy">
  <option value="SuperAdmin">SuperAdmin</option>
  <option value="Admin">Admin</option>
  <option value="User">User</option>
  <option value="ClientAdmin">ClientAdmin</option>
  <option value="ClientUser">ClientUser</option>
</select>

4 个答案:

答案 0 :(得分:0)

您需要用Description属性装饰枚举 例 public enum SomeEnum { [Display(Name="First")] First = 1, [Display(Name="Second")] Second = 2 }

答案 1 :(得分:0)

用说明装饰枚举后,可以使用它:

internal static string GetDescription(Enum value)
{
    var attributes = value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
    return attributes.Length == 0 ? value.ToString() : ((DescriptionAttribute) attributes[0]).Description;
}

答案 2 :(得分:0)

如果只需要SuperAdmin,则可以在下面定义扩展名:

public static class ExtensionMethods
{
    public static IEnumerable<SelectListItem> GetEnumValueSelectList<TEnum>(this IHtmlHelper htmlHelper) where TEnum : struct
    {
        return new SelectList(Enum.GetValues(typeof(TEnum)).OfType<Enum>()
            .Select(x =>
                new SelectListItem
                {
                    Text = x.ToString(),
                    Value = x.ToString()
                }), "Value", "Text");
    }       
}

和用法:

<select asp-items="Html.GetEnumValueSelectList<PolicyEnum>()" class="form-control"></select>

如果您需要[EnumMember(Value = "Admin")],可以尝试

public static class ExtensionMethods
{
    public static IEnumerable<SelectListItem> GetEnumMemberSelectList<TEnum>(this IHtmlHelper htmlHelper) where TEnum : struct
    {
        return new SelectList(Enum.GetValues(typeof(TEnum)).OfType<Enum>()
            .Select(x =>
                new SelectListItem
                {
                    Text = x.EnumMemberValue(),
                    Value = x.ToString()
                }), "Value", "Text");
    }

    public static string EnumMemberValue(this Enum value)
    {
        FieldInfo field = value.GetType().GetField(value.ToString());

        EnumMemberAttribute attribute
                = Attribute.GetCustomAttribute(field, typeof(EnumMemberAttribute))
                    as EnumMemberAttribute;

        return attribute == null ? value.ToString() : attribute.Value;
    }
}

答案 3 :(得分:0)

代替此:

<select asp-for="Policy" asp-items="Html.GetEnumSelectList(typeof(PolicyEnum))" class="form-control"></select>

使用此:

<select asp-for="Policy" asp-items="Html.GetEnumSelectList<Enums.PolicyEnum>()" class="form-control"></select>

并保留显示为枚举。

public enum PolicyEnum
{
     [Display(Name="First")]
     First = 1,
     [Display(Name="Second")]
     Second = 2
} 

在MVC5.1中,您也可以使用此帮助程序:

@Html.EnumDropDownListFor(model => model.PolicyEnum, null, new { @id = "ddlPolicyEnum", @class = "form-control", @style = "width:200px;" })