asp.net-mvc – 本地化数据注释默认消息([必需] [StringLength]等)

如果我使用这样的属性来装饰我的viewmodels的属性
public class Vm
{

[required]
[StringLength(35)]
public string Name {get;set;}

}

我要得到英文验证信息:

"this field is required"
"The field Name must be a string with a maximum length of 35"

我怎么能翻译他们?

解决方法

您可以使用 ErrorMessageResourceName属性
[required(ErrorMessageResourceName = "SomeResource")]
[StringLength(30,ErrorMessageResourceName = "SomeOtherResource")]
public string Name { get; set; }

您可以以this blog post结帐为例。

更新:

在Application_Start中:

DefaultModelBinder.ResourceClassKey = "Messages";

在Messages.resx文件中,您需要添加自定义错误消息。使用反射器查看System.Web.Mvc和System.ComponentModel.DataAnnotations程序集,以查看要使用的键名称

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...