大佬教程收集整理的这篇文章主要介绍了asp.net-mvc – 使用ASP.NET MVC2中的DataAnnotations显示友好的本地化枚举值,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如果我有这样的模型:
public class Mymodel { public MyEnum MyEnumValue { get; set; } }
并在视图中的一行如下:
<%: Html.DisplayFor(model => model.MyEnumvalue) %>
我希望通过DisplayAttribute注释枚举值,如下所示:
public enum MyEnum { [Display(Name="EnumValue1_Name",resourceType=typeof(resources.MyEnumresources))] EnumValue1,[Display(Name="EnumValue2_Name",resourceType=typeof(resources.MyEnumresources))] EnumValue2,[Display(Name="EnumValue3_Name",resourceType=typeof(resources.MyEnumresources))] EnumValue3 }
不支持似乎还有其他需要的东西。实施它最好的方法是什么?
例如。
在视图模型中:
public enum MyEnum { [Description("User Is Sleeping")] Asleep,[Description("User Is Awake")] Awake } public String GetDescription(Type type,String value) { return ((DescriptionAttributE)(type.GetMember(value)[0].GetCustomAttributes(typeof(DescriptionAttributE),falsE)[0])).Description; }
在视图中:
@H_566_4@model.GetDescription(Model.myEnum.GetType(),Model.myEnum) to retrieve the value set in Description.我使用类似的东西来设置我的Html.Dropdown中的displayname和值。
以上是大佬教程为你收集整理的asp.net-mvc – 使用ASP.NET MVC2中的DataAnnotations显示友好的本地化枚举值全部内容,希望文章能够帮你解决asp.net-mvc – 使用ASP.NET MVC2中的DataAnnotations显示友好的本地化枚举值所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。