我能够将其他答案中的一些方法与一些附加功能相结合:
EnumRadioButtonList.cshtml
@model Enum
@foreach (var item in EnumHelper.GetSelectList(Model.GetType()))
{
<div class="radio">
<label>
@Html.RadioButton(string.Empty, item.Value, Model.Equals(Enum.Parse(Model.GetType(), item.Value)))
@item.Text
</label>
</div>
}
这使用EnumHelper.GetSelectList() 作为一种hack,以便您可以对枚举成员使用显示属性:
public enum TestEnum
{
First,
[Display(Name = "Second Member")]
Second,
Third
}
用法:
@Html.EditorFor(m => m.TestEnumProperty, "EnumRadioButtonList")
产量:
Image
我还使用 Bootstrap 单选按钮样式和单独的内联版本帮助器:
EnumRadioButtonListInline.cshtml
@model Enum
@foreach (var item in EnumHelper.GetSelectList(Model.GetType()))
{
<label class="radio-inline">
@Html.RadioButton(string.Empty, item.Value, Model.Equals(Enum.Parse(Model.GetType(), item.Value)))
@item.Text
</label>
}