简体   繁体   English

枚举单选按钮(DataAnnotation)

[英]Radio Button For Enum (DataAnnotation)

I have this 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

I put this EnumDropDownList in my view: 我将此EnumDropDownList放在我的视图中:

@Html.EnumDropDownListFor(model => model.Scholarity, htmlAttributes: new { @class = "form-control" })

It's perfect, showing the name I put in DataAnnotation.. BUT when I try to use this Enum as RadioButton, it doesn't show the name in Display(Name...), show the name of the attribute.. 完美,显示了我在DataAnnotation中输入的名称。但是,当我尝试将此Enum用作RadioButton时,它没有在Display(Name ...)中显示名称,而是显示属性的名称。

Here's my RadioButton: 这是我的RadioButton:

@foreach (var value in Enum.GetValues(typeof(BlaBla.Models.Scholarity)))
{
    @Html.RadioButtonFor(model => model.Scholarity, value)
    <span>@value.ToString()</span> <br />
}

I've already tested Enum.GetNames (without @value.ToString(), just @value).. 我已经测试了Enum.GetNames(没有@ value.ToString(),只有@value)。

Can someone help me? 有人能帮我吗?

Btw, I want it as a Radio Button, because it has a fixed number of elements and has only a few options, so I prefer to use Radio Button. 顺便说一句,我希望将其作为单选按钮,因为它具有固定数量的元素并且只有几个选项,因此我更喜欢使用单选按钮。

Unfortunately there is no out-of-the-box control for this like you get with @Html.EnumDropDownListFor . 不幸的是,没有像@Html.EnumDropDownListFor@Html.EnumDropDownListFor You can, however, create an editor template for radio button enums like mentioned in the following: MVC5: Enum radio button with label as displayname (see selected answer). 但是,您可以为单选按钮枚举创建编辑器模板,如下所述: MVC5:枚举单选按钮,其标签为displayname (请参见选择的答案)。

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

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