加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-15 18:34:14 所属栏目:asp.Net 来源:网络整理
导读:如果我使用这样的属性来装饰我的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" 我怎么
如果我使用这样的属性来装饰我的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程序集,以查看要使用的键名称。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读