asp.net-mvc – Asp.Net MVC 2 – 更改PropertyValueRequired字
发布时间:2020-12-15 23:16:16 所属栏目:asp.Net 来源:网络整理
导读:在App_GlobalResources目录中使用resx文件,我可以更改模型验证器的PropertyValueInvalid字符串的默认消息. 但是,当需要值时,不需要翻译消息(PropertyValueRequired.) 在Global.asax.cs Application_Start()中我已经更改了资源类密钥,如下所示: DefaultModel
在App_GlobalResources目录中使用resx文件,我可以更改模型验证器的PropertyValueInvalid字符串的默认消息.
但是,当需要值时,不需要翻译消息(PropertyValueRequired.) 在Global.asax.cs Application_Start()中我已经更改了资源类密钥,如下所示: DefaultModelBinder.ResourceClassKey = "Messages"; 在Messages.resx文件中,我已经输入了两个条目: >“PropertyValueInvalid”=> “O勇气”{0}“éinválidopara o campo {1}”. 谢谢. 解决方法
RequiredAttribute未使用DefaultModelBinder.GetValueRequiredResource.
创建自定义DataAnnotationsModelValidator类. public class MyRequiredAttributeAdapter : RequiredAttributeAdapter { public MyRequiredAttributeAdapter(ModelMetadata metadata,ControllerContext context,RequiredAttribute attribute) : base(metadata,context,attribute) { attribute.ErrorMessageResourceType = typeof (Messages); attribute.ErrorMessageResourceName = "PropertyValueRequired"; } } 并在Global.asax中注册适配器. DataAnnotationsModelValidatorProvider.RegisterAdapter( typeof(RequiredAttribute),typeof(MyRequiredAttributeAdapter)); 希望这个帮助! Reusable Validation Error Message Resource Strings for DataAnnotations (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- .net – 具有实体框架的存储库模式
- asp.net – 在Entity Framework 5中重命名导航属性不会更新
- asp.net – MVC5中IdentityDbContext和IdentityDbContext之
- asp.net-mvc – MVC4将复杂对象从View传递到Controller,新视
- asp.net – MVC动态页权限使用授权属性?
- asp.net – 异步HttpWebRequest,从Web应用程序中无需等待
- asp.net-web-api – ASP.NET核心WebAPI 404错误
- ASP.NET无法访问IIS元数据库
- .net core 3.0web_razor page项目_使用中间件接受大文件上传
- asp.net – 什么时候调用Application_End,我该如何手动导致