Cyb*_*cop 14 c# asp.net-mvc enums asp.net-mvc-5
我有这些枚举
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
}
Run Code Online (Sandbox Code Playgroud)
我有一个帮助类来显示枚举中每个字段的所有单选按钮
@model Enum
@foreach (var value in Enum.GetValues(Model.GetType()))
{
@Html.RadioButtonFor(m => m, value)
@Html.Label(value.ToString())
<br/>
}
Run Code Online (Sandbox Code Playgroud)
现在标签设置为值名称,而不是我为值给出的显示名称.
例如:
[Display(Name="Cancel Invitation")]
CancelInvitation
Run Code Online (Sandbox Code Playgroud)
我CancelInvitation旁边有单选按钮.
如何让它显示我给它的显示名称(Cancel Invitation)?
ram*_*ilu 25
这是解决方案 -
归功于这位非凡的绅士--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<Enum, string> 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<Enum>().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++);
<div class="editor-radio">
@Html.RadioButton(prefix, li.Value, li.Selected, new { @id = fieldName })
@Html.Label(fieldName, li.Text)
</div>
}
ViewData.TemplateInfo.HtmlFieldPrefix = prefix;
}
Run Code Online (Sandbox Code Playgroud)
然后有你的枚举 -
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
}
Run Code Online (Sandbox Code Playgroud)
第2步 -创建模型 -
public class RadioEnumModel
{
public QuestionEnd qEnd { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
第3步 -创建控制器操作 -
public ActionResult Index()
{
RadioEnumModel m = new RadioEnumModel();
return View(m);
}
Run Code Online (Sandbox Code Playgroud)
第4步 -创建视图 -
@model MVC.Controllers.RadioEnumModel
@Html.EditorFor(x => x.qEnd, "RadioButtonListEnum")
Run Code Online (Sandbox Code Playgroud)
然后输出将是 -

| 归档时间: |
|
| 查看次数: |
14162 次 |
| 最近记录: |