考虑枚举
公共enum教育 {
[Display(Name="display for education 1")]
Education1, [Display(Name="display for education 2")] Education2, [Display(Name="display for education 3")] Education3, }
现在,我希望下拉显示值
@Html.DropDownListFor(
model => model.Education,
new SelectList(@Enum.GetValues(typeof(Education)))
new {@class = "default"}
)
我想要显示名称(目前我得到了枚举字符串本身。 有人知道如何在.cshtml中提取它?
答案 0 :(得分:0)
看看这个
public static string[] GetEnumDisplays(Type type)
{
var displays = new List<string>();
foreach (FieldInfo f in type.GetFields(BindingFlags.Public | BindingFlags.Static))
{
object[] attr = f.GetCustomAttributes(typeof(DisplayAttribute), false);
if (attr.Length != 0)
{
var displayAttribute = (DisplayAttribute)attr[0];
displays.Add(displayAttribute.Name);
}
else
{
displays.Add(f.Name);
}
}
return displays.ToArray();
}