在MVC3项目中,我使用了一个带有display-Project的枚举:
public enum Foo {
[Display(Name = "Undefined")]
Undef = 0,
[Display(Name = "Fully colored")]
Full = 1
}
模型类有一个使用此枚举的属性:
public Foo FooProp { get; set; }
该视图使用model类,并通过
@Html.DisplayFor(m => m.FooProp)
现在,最后,我的问题是:
如何让.DisplayFor()显示显示属性中的字符串,而不是只显示枚举的值名?(它应该显示"Undefined“或”Full colored",但显示"Undef“或"Full")。
谢谢你的提示!
发布于 2011-06-06 04:21:42
自定义显示模板可能会有所帮助(~/Views/Shared/DisplayTemplates/Foo.cshtml
):
@using System.ComponentModel.DataAnnotations
@model Foo
@{
var field = Model.GetType().GetField(Model.ToString());
if (field != null)
{
var display = ((DisplayAttribute[])field.GetCustomAttributes(typeof(DisplayAttribute), false)).FirstOrDefault();
if (display != null)
{
@display.Name
}
}
}
https://stackoverflow.com/questions/6245074
复制相似问题