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 MVC 1.0中实现与ASP.NET MVC
- .net – 有人有一个例子,说明为什么我会主持一个WCF服务
- asp.net-mvc – 在MVC应用程序中使用ELMAH调试/跟踪消息
- asp.net – Response.IsClientConnected如何在幕后工作?
- asp.net – 如何删除日历的最后一周
- asp.net-mvc – 为什么Hangfire需要身份验证才能查看仪表板
- asp.net-mvc – 获取Controller.OnException中的Action和Ac
- asp.net-mvc-3 – 从ASP .NET MVC3调用WCF服务的最佳实践是
- asp.net-mvc – mvc HtmlHelper DisplayFor函数如何从lambd
- asp.net – Python / web scrape / aspx – 没有表格时人性
- asp.net – 向GridView Row添加ID
- asp.net – Visual Studio发布/或构建解决方案ZI
- 如何使用asp.net将.docx转换为html?
- asp.net-core – 使用NLog记录请求正文和请求标头
- asp.net-mvc – ASP.NET MVC中的WebApi [FromUri
- asp.net – 无法编写输出文件… pdb visual stud
- 在ASP.NET MVC中创建全局对象的最佳位置
- asp.net – IIS 7的最大默认POST请求大小 – 如何
- asp.net-mvc-3 – 使用Quartz.Net的MVC 3
- asp.net-mvc-3 – 在控制器操作完成后使用Javasc