我有这个enum:
[Display(Name = "Primary incomplete")]
PrimaryIncomplete = 1,
[Display(Name = "Primary")]
Primary = 2,
[Display(Name = "Secondary incomplete")]
SecondaryIncomplete = 3,
[Display(Name = "Secondary")]
Secondary = 4,
[Display(Name = "Higher education incomplete")]
HigherEducationIncomplete = 5,
[Display(Name = "Higher education")]
HigherEducation = 6,
[Display(Name = "Post-Graduation/MBA")]
PostGraduationMBA = 7
我把这个EnumDropDownList放到我的视图中:
@Html.EnumDropDownListFor(model => model.Scholarity, htmlAttributes: new { @class = "form-control" })
它很完美,显示了我在DataAnnotation中放入的名称。但是当我尝试使用这个Enum作为RadioButton时,它不显示显示(name…)中的名称,显示属性的名称…
这是我的RadioButton:
@foreach (var value in Enum.GetValues(typeof(BlaBla.Models.Scholarity)))
{
@Html.RadioButtonFor(model => model.Scholarity, value)
<span>@value.ToString()</span> <br />
}
我已经测试了Enum。GetNames(没有@value. tostring(),只有@value).
有人能帮帮我吗?
顺便说一句,我希望它是一个单选按钮,因为它有固定数量的元素,只有几个选项,所以我更喜欢使用Radio Button
不幸的是,没有像@Html.EnumDropDownListFor
那样的开箱即用的控件。但是,您可以为单选按钮枚举创建一个编辑器模板,如下所述:MVC5:标签为displayname的Enum单选按钮(参见所选答案)