如何使用和/或与ASP.NET MVC2本地化DisplayAttribute? [英] How to use and/or localize DisplayAttribute with ASP.NET MVC2?
问题描述
可能重复:结果
从资源 displayName属性?
块引用>我试图找出如何让DisplayAttribute在我的MVC 2视图模型与Html.LabelFor()辅助工作。
无论
公共类TestModel
{
[显示(=的ResourceType typeof运算(Localization.Labels))]
公共字符串文本{搞定;组; }
}也没有
公共类TestModel
{
[显示(名称=测试)]
公共字符串文本{搞定;组; }
}似乎工作。本地化所需的属性按预期工作:
[必需的(ErrorMessageResourceName =测试,ErrorMessageResourceType = typeof运算(Localization.Labels))]
我使用VS2010 RC。有没有人得到跑跑?
解决方案[显示]属性是一个.NET 4的具体属性。由于MVC 2对.NET 3.5编译,运行时不承认这个属性。
请参阅 HTTP://aspnet.$c$cplex.com /WorkItem/View.aspx?WorkItemId=5515 了解信息以及解决方法。
编辑:
呃,工作项目并不是那么大。可能也包括它内联。 :)
[显示]属性是新的
DataAnnotations v4的,所以MVC 2不能使用
这是因为我们对编译
DataAnnotations v3.5版本。使用
[显示名称],而不是直到MVC 3,
在这里我们将针对编译
DataAnnotations V4。
您有几个解决方法。当.NET
4的RTM,我们将提供一个.NET
4专用期货二进制,而
期货二进制将有一个元数据
供应商能够理解[显示]
和其他DataAnnotations V4专用
属性。另外,如果您需要
一个解决方案马上,子类
[显示名称]属性,使
私人DisplayNameAttribute场
这是适当的实例化,
重写虚
DisplayNameAttribute.DisplayName
属性,以便它委托给
_theWrappedDisplayNameAttribute.GetName()。
块引用>公共类MultiCulturalDisplayName:DisplayNameAttribute {
私人DisplayAttribute显示; 公共MultiCulturalDisplayName(resourceType为型,字符串资源名称){
this.display =新DisplayAttribute {=的ResourceType resourceType为,名称=资源名称};
} 公众覆盖字符串显示名称{
{返回display.GetName(); }
}
}Possible Duplicate:
DisplayName attribute from Resources?I am trying to figure out how to get the DisplayAttribute in my MVC 2 ViewModel to work with the Html.LabelFor() helper.
Neither
public class TestModel { [Display(ResourceType = typeof(Localization.Labels))] public string Text { get; set; } }
nor
public class TestModel { [Display(Name = "test")] public string Text { get; set; } }
seem to work. Localizing the Required Attribute works as expected:
[Required(ErrorMessageResourceName = "Test", ErrorMessageResourceType = typeof(Localization.Labels))]
I am using VS2010 RC. Has anybody got that running?
解决方案The [Display] attribute is a .NET 4-specific attribute. Since MVC 2 is compiled against .NET 3.5, the runtime does not recognize this attribute.
See http://aspnet.codeplex.com/WorkItem/View.aspx?WorkItemId=5515 for more information plus workarounds.
Edit:
Eh, the work item's not that big. May as well include it inline. :)
The [Display] attribute is new in DataAnnotations v4, so MVC 2 can't use it because we're compiled against DataAnnotations v3.5. Use [DisplayName] instead until MVC 3, where we will be compiled against DataAnnotations v4.
You have a few workarounds. When .NET 4 RTMs, we will provide a .NET 4-specific Futures binary, and that Futures binary will have a metadata provider that understands [Display] and other DataAnnotations v4-specific attributes. Alternatively, if you need a solution right away, subclass the [DisplayName] attribute, make a private DisplayNameAttribute field that's instantiated appropriately, and override the virtual DisplayNameAttribute.DisplayName property so that it delegates to _theWrappedDisplayNameAttribute.GetName().
public class MultiCulturalDisplayName : DisplayNameAttribute { private DisplayAttribute display; public MultiCulturalDisplayName(Type resourceType, string resourceName) { this.display = new DisplayAttribute { ResourceType = resourceType, Name = resourceName }; } public override string DisplayName { get { return display.GetName(); } } }
这篇关于如何使用和/或与ASP.NET MVC2本地化DisplayAttribute?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!