如何通过 ASP.NET MVC2 使用和/或本地化 DisplayAttribute?
可能的重复:
资源中的 DisplayName 属性?
我试图弄清楚如何在 MVC 2 中获取 DisplayAttribute ViewModel 与 Html.LabelFor() 帮助器一起使用。
不起作用
public class TestModel
{
[Display(ResourceType = typeof(Localization.Labels))]
public string Text { get; set; }
}
似乎都
public class TestModel
{
[Display(Name = "test")]
public string Text { get; set; }
}
。本地化所需属性按预期工作:
[Required(ErrorMessageResourceName = "Test", ErrorMessageResourceType = typeof(Localization.Labels))]
我正在使用 VS2010 RC。有人运行过吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
[Display] 属性是 .NET 4 特定的属性。由于 MVC 2 是针对 .NET 3.5 编译的,因此运行时无法识别此属性。
请参阅 http://aspnet.codeplex.com/WorkItem/View.aspx?WorkItemId =5515 了解更多信息和解决方法。
编辑:
呃,工作项没那么大。也可以将其内联包含在内。 :)
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. :)
如果您下载 ASP.NET MVC 2 Futures 程序集,则可以使用 DisplayAttribute。您只需将
DataAnnotations4ModelMetadataProvider.RegisterProvider();
添加到 Global.asax.csIf you download ASP.NET MVC 2 Futures assembly, then you can use the DisplayAttribute. You just need to add
DataAnnotations4ModelMetadataProvider.RegisterProvider();
to your Global.asax.csLevi 几乎回答了你的问题,这里记录的是 3.5 的工作版本
Levi pretty much answered your question, and for the record here is a working version for 3.5