如果我使用这样的属性来装饰我的viewmodels的属性:
public class Vm { [@R_404_103@] [StringLength(35)] public string Name {get;set;} }@H_403_3@我要得到英文验证信息:
"this field is @R_404_103@" "The field Name must be a string with a maximum length of 35"@H_403_3@我怎么能翻译他们?
解决方法
您可以使用
ErrorMessageResourceName
属性:
[@R_404_103@(ErrorMessageResourceName = "SomeResource")] [StringLength(30,ErrorMessageResourceName = "SomeOtherResource")] public string Name { get; set; }@H_403_3@您可以以this blog post结帐为例。
更新:
在Application_Start中:
DefaultModelBinder.ResourceClassKey = "Messages";@H_403_3@在Messages.resx文件中,您需要添加自定义错误消息。使用反射器查看System.Web.Mvc和System.ComponentModel.DataAnnotations程序集,以查看要使用的键名称。