大佬教程收集整理的这篇文章主要介绍了asp.net-mvc-3 – 如何使MVC3 DisplayFor显示枚举显示属性的值?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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”或“全部”)。
感谢提示!
@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-3 – 如何使MVC3 DisplayFor显示枚举显示属性的值?全部内容,希望文章能够帮你解决asp.net-mvc-3 – 如何使MVC3 DisplayFor显示枚举显示属性的值?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。