asp.net-mvc-3 – 如何使MVC3 DisplayFor显示枚举显示属性的值?
发布时间:2020-12-16 00:27:26 所属栏目:asp.Net 来源:网络整理
导读:在MVC3项目中,我正在使用带有display-Attributes的枚举: public enum Foo { [Display(Name = "Undefined")] Undef = 0,[Display(Name = "Fully colored")] Full = 1} 模型类有一个使用此枚举的属性: public Foo FooProp { get; set; } 该视图使用模型类并
在MVC3项目中,我正在使用带有display-Attributes的枚举:
public enum Foo { [Display(Name = "Undefined")] Undef = 0,[Display(Name = "Fully colored")] Full = 1 } 模型类有一个使用此枚举的属性: public Foo FooProp { get; set; } 该视图使用模型类并通过显示属性 @Html.DisplayFor(m => m.FooProp) 现在,最后我的问题: 我如何做.DisplayFor()显示来自Display-Attribute的字符串,而不是仅显示枚举的值名称? (应该显示“未定义”或“全色”,但显示“Undef”或“全部”)。 感谢提示! 解决方法
自定义显示模板可能有帮助(?/ 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 } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 我如何编写一个ActionFilter来确保AntiForg
- asp.net-web-api – ASP.NET Web API:如何在Web API控制器
- asp.net – CKEditor在更新面板中无法正常工作
- asp.net-mvc – 在默认路由的ASP.NET MVC中的Url.Content的
- asp.net-web-api – 如何在ConfigureServices方法中获取IOp
- ASP / C#会话变量 – 未将对象引用设置为对象的实例
- 走进 .Net 单元测试
- 使用离线功能构建ASP.Net Web应用程序
- asp.net-mvc-2 – 我可以从global.asax重定向到控制器操作吗
- asp.net-mvc – MVC Razor获取月份名称
推荐文章
站长推荐
- ASP.NET / VB.NET:Dropdownlist SelectedIndexC
- asp.net – 是否有使用Markdown作为编辑器的WIKI
- ASP.NET知识点总结3
- asp.net-mvc – 不需要必需属性的bool属性
- asp.net – System.Net.Cookie和System.Web.Http
- asp.net – IIS 7错误请求响应时 存在于URL中
- 如何使用ASP.NET Core自定义Tag Helper中的视图?
- 在asp.net中使用太多会话的缺点
- asp.net – 如何在抛出请求验证异常时显示自定义
- asp.net-mvc – 在stackoverflow.com上永不过期的
热点阅读