Asp.net mvc 在ASP.NET MVC2中使用DataAnnotation显示友好的本地化枚举值
在MVC2中显示本地化枚举属性的推荐方法是什么 如果我有这样一个模型:Asp.net mvc 在ASP.NET MVC2中使用DataAnnotation显示友好的本地化枚举值,asp.net-mvc,asp.net-mvc-2,Asp.net Mvc,Asp.net Mvc 2,在MVC2中显示本地化枚举属性的推荐方法是什么 如果我有这样一个模型: public class MyModel { public MyEnum MyEnumValue { get; set; } } <%: Html.DisplayFor(model => model.MyEnumValue) %> public enum MyEnum { [Display(Name="EnumValue1_Name", ResourceType=typeof(Resour
public class MyModel {
public MyEnum MyEnumValue { get; set; }
}
<%: Html.DisplayFor(model => model.MyEnumValue) %>
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 class MyModel {
public MyEnum MyEnumValue { get; set; }
}
<%: Html.DisplayFor(model => model.MyEnumValue) %>
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
}
这是不受支持的。看来还需要别的东西。最好的实现方法是什么?您可以尝试使用DescriptionAttribute来实现此功能 例如 在视图模型中:
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;
}
他认为:
Model.GetDescription(Model.myEnum.GetType(), Model.myEnum) to retrieve the value set in Description.
我正在使用类似的方法来设置Html.Dropdown中的displayname和value。我还使用Display注释。下面是我最后使用的,它对属性和枚举成员都有效 这是我的枚举:
public enum TagOrderStatus
{
[Display(ResourceType = typeof(TagStrings), Name = "TagOrderStatus_NotOrdered")]
NotOrdered = 0,
[Display(ResourceType = typeof(TagStrings), Name = "TagOrderStatus_ToOrder")]
ToOrder = 1,
[Display(ResourceType = typeof(TagStrings), Name = "TagOrderStatus_Ordered")]
Ordered = 2
}
然后,我的“一劳永逸”实用方法:
public static string GetLocalizedDisplay<TModel>(string pPropertyName)
{
DisplayAttribute attribute;
if (typeof(TModel).IsEnum)
{
MemberInfo member = typeof(TModel).GetMembers().SingleOrDefault(m => m.MemberType == MemberTypes.Field && m.Name == pPropertyName);
attribute = (DisplayAttribute)member.GetCustomAttributes(typeof(DisplayAttribute), false)[0];
}
else
{
PropertyInfo property = typeof(TModel).GetProperty(pPropertyName);
attribute = (DisplayAttribute)property.GetCustomAttributes(typeof(DisplayAttribute), true)[0];
}
if (attribute.ResourceType != null)
return new ResourceManager(attribute.ResourceType).GetString(attribute.Name);
else
return attribute.Name;
}
公共静态字符串GetLocalizedDisplay(字符串PPPropertyName)
{
显示属性;
if(typeof(TModel).IsEnum)
{
MemberInfo member=typeof(TModel).GetMembers().SingleOrDefault(m=>m.MemberType==MemberTypes.Field&&m.Name==pPropertyName);
attribute=(DisplayAttribute)member.GetCustomAttributes(typeof(DisplayAttribute),false)[0];
}
其他的
{
PropertyInfo property=typeof(TModel).GetProperty(pPropertyName);
attribute=(DisplayAttribute)property.GetCustomAttributes(typeof(DisplayAttribute),true)[0];
}
if(attribute.ResourceType!=null)
返回新的ResourceManager(attribute.ResourceType).GetString(attribute.Name);
其他的
返回属性.Name;
}
然后可以通过这种方式获取枚举成员的单个成员显示属性:
string disp = GetLocalizedDisplay<Tag.TagOrderStatus>("Ordered");
string disp=GetLocalizedDisplay(“有序”);
或财产:
string disp = GetLocalizedDisplay<Tag>("OwnerName");
string disp=GetLocalizedDisplay(“所有者名称”);
我喜欢仿制药。希望这有帮助 您希望显示什么作为最终结果?资源文件中的适当翻译,即EnumValue1_名称等。请查看此问题,它可能有用!当然,但这不是本地化的。视图需要知道它们是在显示枚举还是其他内容,我希望将这一问题分开。我希望扩展ModelMetadataProvider或类似的东西。如果我找到答案,我会把它贴出来。是的,你是对的。这对本地化请求不起作用。不过我会保留这个职位的。有些人可能会发现它对其他场景很有用。