asp.net-mvc – 使用与本地化更改冲突的自定义数据注释进行验证
我正在使用我的MVC 4应用程序中的数据注释来处理验证.这样做的一个要求是完全本地化所有错误消息和正则表达式. 为了做到这一点,我写了一个属性扩展,如下所示. 视图: @Html.LabelFor(m => m.Postcode,new { @class = "input-label",@for = "valid-postcode" }) @Html.TextBoxFor(m => m.Postcode,new { type = "text",id = "valid-postcode",autocomplete = "off" }) @Html.ValidationMessageFor(m => m.Postcode) 模型: // Postcode [Required(ErrorMessageResourceType = typeof(Resources.FormValidation),ErrorMessageResourceName = "requiredMsg")] [Localised(typeof(Resources.FormValidation),"postcodeRegEx","postcodeMsg")] [Display(Name = "postcode",ResourceType = typeof(Resources.FormLabels))] public string Postcode { get; set; } 属性扩展: public class LocalisedAttribute : RegularExpressionAttribute { public LocalisedAttribute(Type resource,string regularExpression,string errorMessage) : base(Resources.FormValidation.ResourceManager.GetString(regularExpression)) { ErrorMessageResourceType = resource; ErrorMessageResourceName = errorMessage; } } 如果我在属性扩展上设置断点并启动应用程序,当我查看包含表单元素的页面时,我会点击断点.在我测试的时候,我添加了一个额外的字段,它也使用了相同的扩展名,然后两次点击断点.所以从这里,我知道它正在工作,我知道它从我的资源文件中获取正则表达式. 问题 我有一个菜单来切换应用程序中使用的文化.当我选择新文化并刷新页面时,对我的视图中使用的资源文件的所有引用以及数据注释中的显示名称和错误消息都会选择文化更改并使用正确的资源文件. 但是,正则表达式未更新,并且我设置的断点不会再次被触发.这意味着我的扩展程序仍在使用它在被命中时拾取的正则表达式,因此无法正确验证. 如果需要,我可以从这个菜单发布更多有关文化变化的细节,但基本结构是 >控制器,用于更改区域性并将用户返回到同一页面 我需要的是每次切换文化时都会触发我的属性扩展,而不仅仅是第一次启动应用程序. 这是可能的,还是我应该修改我的整个方法? 解决方法您的菜单字符串在运行时被引用,而您的属性在应用程序运行之前编译我能理解为什么这会令人困惑. 资源文件基本上用于动态行为.字符串值可以在运行时更改. 但是,当我们深入研究这个特定字符串的用法时,您使用Resources.FormValidation.ResourceManager.GetString(regularExpression)资源字符串作为编译指令的一部分来创建Postcode. Razor Framework将使用此数据创建用于验证的注释模板. [Required(ErrorMessageResourceType = typeof(Resources.FormValidation),ResourceType = typeof(Resources.FormLabels))] public string Postcode { get; set; } 您在COMPILE TIME使用此字符串postcodeRegEx字符串: 在某些情况下,如果字符串更改,依赖于字符串文字的编译和预编译代码可能会有不同的行为.在其他情况下,例如验证属性行为,您无法轻松地“重新编译”对象的行为. 可能的解决方案 要实现这种“终结”,你必须超越标准 1)实现验证属性(ValidationAttribute)的扩展,继承自RegularExpressionAttribute,它从资源文件中读取特定的RegEx字符串并将其传递给基本的RegEx属性. // New attribute loads RegEx when needed [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property,AllowMultiple = false)] public class LocalisedAttribute : RegularExpressionAttribute { static LocalizedRegexAttribute() { // necessary to enable client side validation DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(LocalizedRegexAttribute),typeof(RegularExpressionAttributeAdapter)); } public LocalisedAttribute(Type resource,string regularExpressionKey,string errorMessage) : base(LoadRegex(regularExpressionKey)) { ErrorMessageResourceType = resource; ErrorMessageResourceName = errorMessage; } private static string LoadRegex(string key) { var resourceManager = new ResourceManager(typeof(Resources.FormValidation)); return resourceManager.GetString(key); } } 2)使用JQuery生成输入数据-val-regex-pattern = @ ViewBag.RegEx 它将引用JQuery函数 $.validator.unobtrusive.adapters.add('Postcode ',function(options) { /*...*/ }); 我怀疑Postcode输入的data-val-regex-pattern将被设置为初始资源文件中的值. (编辑:甘南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – ASP.NET MVC3中的随机会话超时
- asp.net-mvc – 防止在ASP.NET MVC中缓存属性,每次执行一个
- asp.net – 在剃刀中等同于End / Response.End?
- asp.net – 从我的GridView行返回一个对象
- asp.net-identity交易问题
- asp.net中XML如何做增删改查操作
- asp.net-mvc – ActionResult上的自定义属性
- ASP.NET和System.Diagnostics跟踪 – 我错过了什么,或者这是
- Asp.net mvc验证用户登录之Forms实现详解
- asp.net-mvc – Domain Driven Design新手,请简要解释’价值
- asp.net-mvc-4 – 在EF迁移配置类的Seed方法中获
- asp.net-mvc-3 – “区域”文件夹中的样式,脚本和
- asp.net-mvc – ASP.NET MVC检查Controller或Act
- asp.net – 在Web.config帮助中定义tagPrefixes
- 实体框架 – 使用EF和WebAPI,如何返回一个ViewMo
- ASP.Net中的图形(c#)
- asp.net – ASPXAUTH cookie未保存
- asp.net-mvc – 在asp.net mvc中启动一组未选中的
- asp.net-mvc – ASP.NET MVC忽略所有url结尾的“
- asp.net-mvc – ASP.NET MVC/C++#:可以使用Html