如果我使用这样的属性来装饰我的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程序集,以查看要使用的键名称。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。