枚举单选按钮(DataAnnotation)

时间:2015-05-26 01:51:21

标签: c# enums radio-button data-annotations html-helper

我有这个枚举:

[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时,它不会在Display(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)..

有人可以帮助我吗?

顺便说一下,我想把它当作一个单选按钮,因为它有固定数量的元素,只有几个选项,所以我更喜欢使用单选按钮。

1 个答案:

答案 0 :(得分:0)

不幸的是,与ShopOrder一样,没有开箱即用的控制权。但是,您可以为单选按钮枚举创建一个编辑器模板,如下所述:MVC5: Enum radio button with label as displayname(请参阅选定的答案)。