MVC5:Enum单选按钮,标签为displayname
我有这些枚举
public enum QuestionStart { [Display(Name="Repeat till common match is found")] RepeatTillCommonIsFound, [Display(Name="Repeat once")] RepeatOnce, [Display(Name="No repeat")] NoRepeat } public enum QuestionEnd { [Display(Name="Cancel Invitation")] CancelInvitation, [Display(Name="Plan with participants on first available common date")] FirstAvailableCommon, [Display(Name="Plan with participants on my first available common date")] YourFirstAvailableCommon }
我有一个帮助类来显示枚举中每个字段的所有单选按钮
@model Enum @foreach (var value in Enum.GetValues(Model.GetType())) { @Html.RadioButtonFor(m => m, value) @Html.Label(value.ToString())
}
现在标签设置为值名称,而不是我为值给出的显示名称。
例如:
[Display(Name="Cancel Invitation")] CancelInvitation
我在旁边有CancelInvitation
单选按钮。
如何让它显示我给它的显示名称( Cancel Invitation
)?
这是解决方案 –
归功于这位非凡的绅士–ThumNet,他为Enum编写了RadioButtonList作为扩展
步骤1 –在Views / Shared / EditorTemplates目录中创建带有以下代码(来自上面的参考的代码)的RadioButtonListEnum.cshtml文件(如果不存在,则创建该目录) –
@model Enum @{ // Looks for a [Display(Name="Some Name")] or a [Display(Name="Some Name", ResourceType=typeof(ResourceFile)] Attribute on your enum Func getDescription = en => { Type type = en.GetType(); System.Reflection.MemberInfo[] memInfo = type.GetMember(en.ToString()); if (memInfo != null && memInfo.Length > 0) { object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute), false); if (attrs != null && attrs.Length > 0) return ((System.ComponentModel.DataAnnotations.DisplayAttribute)attrs[0]).GetName(); } return en.ToString(); }; var listItems = Enum.GetValues(Model.GetType()).OfType().Select(e => new SelectListItem() { Text = getDescription(e), Value = e.ToString(), Selected = e.Equals(Model) }); string prefix = ViewData.TemplateInfo.HtmlFieldPrefix; int index = 0; ViewData.TemplateInfo.HtmlFieldPrefix = string.Empty; foreach (var li in listItems) { string fieldName = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}_{1}", prefix, index++); @Html.RadioButton(prefix, li.Value, li.Selected, new { @id = fieldName }) @Html.Label(fieldName, li.Text) } ViewData.TemplateInfo.HtmlFieldPrefix = prefix; }
然后有你的枚举 –
public enum QuestionEnd { [Display(Name = "Cancel Invitation")] CancelInvitation, [Display(Name = "Plan with participants on first available common date")] FirstAvailableCommon, [Display(Name = "Plan with participants on my first available common date")] YourFirstAvailableCommon }
第2步 –创建模型 –
public class RadioEnumModel { public QuestionEnd qEnd { get; set; } }
第3步 –创建控制器操作 –
public ActionResult Index() { RadioEnumModel m = new RadioEnumModel(); return View(m); }
第4步 –创建视图 –
@model MVC.Controllers.RadioEnumModel @Html.EditorFor(x => x.qEnd, "RadioButtonListEnum")
然后输出将是 –
我发现其中一些答案令人困惑,这就是我最终实现结果的方式。 希望这有助于其他人。
在扩展方法文件中推送它:
public static string GetDescription(this Enum value) { Type type = value.GetType(); string name = Enum.GetName(type, value); if (name != null) { FieldInfo field = type.GetField(name); if (field != null) { DescriptionAttribute attr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; if (attr != null) { return attr.Description; } } } return null; }
确保在实际模型中添加与枚举类型相同的属性,以便选择单选按钮。
为您的特定枚举创建一个编辑器模板。 然后在您的视图中引用它如下:
@Html.EditorFor(m => m.MyEnumTypePropertyName)
然后将以下内容添加到EditorTemplate:
@using MyProject.ExtensionMethods; @model MyProject.Models.Enums.MyEnumType @foreach (MyEnumType value in Enum.GetValues(typeof(MyEnumType))) { @Html.Label(value.GetDescription()) @Html.RadioButtonFor(m => m, value) }
记住 – 如果由于某种原因没有选择单选按钮,发布时枚举属性的默认值将始终是包中的第一个(即零),而不是null。
您可以使用以下重载方法。
@Html.Label(value.ToString(),"Cancel Invitation")
这将使标签具有指定的labeltext作为上述调用中的第二个参数。
这是一个解决方案,它使用扩展方法和编辑器模板从display属性创建一个具有本地化名称的无线电组。
扩展方法
public static string DisplayName(this Enum enumValue) { var enumType = enumValue.GetType(); var memberInfo = enumType.GetMember(enumValue.ToString()).First(); if (memberInfo == null || !memberInfo.CustomAttributes.Any()) return enumValue.ToString(); var displayAttribute = memberInfo.GetCustomAttribute(); if (displayAttribute == null) return enumValue.ToString(); if (displayAttribute.ResourceType != null && displayAttribute.Name != null) { var manager = new ResourceManager(displayAttribute.ResourceType); return manager.GetString(displayAttribute.Name); } return displayAttribute.Name ?? enumValue.ToString(); }
例
public enum IndexGroupBy { [Display(Name = "By Alpha")] ByAlpha, [Display(Name = "By Type")] ByType }
这是它的用法:
@IndexGroupBy.ByAlpha.DisplayName()
编辑模板
这是一个可以与上面的扩展方法一起使用的编辑器模板:
@model Enum @{ var listItems = Enum.GetValues(Model.GetType()).OfType().Select(e => new SelectListItem { Text = e.DisplayName(), Value = e.ToString(), Selected = e.Equals(Model) }); var prefix = ViewData.TemplateInfo.HtmlFieldPrefix; var index = 0; ViewData.TemplateInfo.HtmlFieldPrefix = string.Empty; foreach (var li in listItems) { var fieldName = string.Format(CultureInfo.InvariantCulture, "{0}_{1}", prefix, index++); @Html.RadioButton(prefix, li.Value, li.Selected, new {@id = fieldName}) @Html.Label(fieldName, li.Text) } ViewData.TemplateInfo.HtmlFieldPrefix = prefix; }
以下是一个示例用法:
@Html.EditorFor(m => m.YourEnumMember, "Enum_RadioButtonList")