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将被设置为初始资源文件中的值. (编辑:4S站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – CalendarExtender定位问题
- .net – 如何正确处理n层应用程序中的错误?
- 认证 – asp.net mvc 3:Page.User.IsInRole(“xy”)返回nu
- 如果我的Asp.Net会话有IsNewSession == true,那我的意思是什
- asp.net – 使用JavaScript重新排列的ListBox元素导致回发时
- ASP.Net MVC cookies – 防篡改?
- 端到ASP.NET MVC的推荐方法
- asp.net-mvc-routing – 在MVC 6控制器中使用urlhelper生成
- asp.net – 登录后对Membership.GetAllUsers()的例外情况:
- asp.net-mvc – ASP.NET MVC:在其中生成带有自定义html的动
- asp.net-mvc – 在ASP.NET MVC中获取当前操作/控
- asp.net – NHibernate – ManagedWebSessionCon
- asp.net – IIS Web Garden中的Singleton对象
- asp.net-mvc – 为什么MVC4捆绑捆绑Knockout.js?
- asp.net – $(“#dialog”).parent().appendTo($
- asp.net 文件上传实例汇总
- ASP.NET linkbutton两次提高onBeforeUnload事件
- asp.net-mvc – 如何在ASP.NET MVC4中使用具有唯
- asp.net Web.config 详细配置说明
- 我如何让Fiddler捕获我的MVC应用程序向我的ASP.N