簡體   English   中英

枚舉單選按鈕(DataAnnotation)

[英]Radio Button For Enum (DataAnnotation)

我有這個枚舉:

[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)。

有人能幫我嗎?

順便說一句,我希望將其作為單選按鈕,因為它具有固定數量的元素並且只有幾個選項,因此我更喜歡使用單選按鈕。

不幸的是,沒有像@Html.EnumDropDownListFor@Html.EnumDropDownListFor 但是,您可以為單選按鈕枚舉創建編輯器模板,如下所述: MVC5:枚舉單選按鈕,其標簽為displayname (請參見選擇的答案)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM