asp.net-mvc-3 – 自定义RegularExpressionAttribute缺少数据-va
我创建了以下自定义RegularExpressionAttribute
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field,AllowMultiple = false)] public class AlphaNumericAttribute: RegularExpressionAttribute,IClientValidatable { public AlphaNumericAttribute() : base("^[-A-Za-z0-9]+$") { } public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata,ControllerContext context) { yield return new ModelClientValidationRule { ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),ValidationType = "alphanumeric" }; } } ViewModel中的字段使用我的AlphaNumeric属性进行修饰: [AlphaNumeric(ErrorMessageResourceType = typeof(Resources),ErrorMessageResourceName = Resources.DriverLicenseNumber_RegexError_)] public string DriverLicenseNumber { get; set; } 该字段构建在视图中: @using (Html.BeginForm("Index","Application",FormMethod.Post,new { id = "applicationDataForm",autocomplete = "off" })) { @Html.LabelFor(m => m.DriverLicenseNumber) @Html.ValidationMessageFor(m => m.DriverLicenseNumber) @Html.TextBoxFor(m => m.DriverLicenseNumber) } 这应该在我的html输入标记上产生适当的“数据 – ”验证属性.但是,呈现的标记如下所示: <input data-val="true" data-val-alphanumeric="Please enter a valid driver's license number." id="DriverLicenseNumber" name="DriverLicenseNumber" type="text" value="" maxlength="20" class="valid"> 显然不存在应该呈现的data-val-regex和data-val-regex-pattern属性. 我已经构建了具有完全相同结构的其他验证器,并且它们正常工作,就像这个SSN验证一样,它使用jquery掩码处理屏蔽输入的屏蔽空间: public class SsnAttribute : RegularExpressionAttribute,IClientValidatable { public SsnAttribute() : base("^([0-9]{3}–[0-9]{2}–[0-9]{4})|([?]{3}–[?]{2}–[?]{4})|([0-9]{9,9})$") { } public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata,ControllerContext context) { yield return new ModelClientValidationRule { ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),ValidationType = "ssn" }; } } 随着ViewModel上附带的应用程序: [Ssn(ErrorMessageResourceType = typeof(Resources),ErrorMessageResourceName = Resources.SocialSecurity_RegexError_)] public new string SocialSecurityNumber { get; set; } 该字段构建在视图中: @using (Html.BeginForm("Index",autocomplete = "off" })) { @Html.LabelFor(m => m.SocialSecurityNumber) @Html.ValidationMessageFor(m => m.SocialSecurityNumber) @Html.TextBoxFor(m => m.SocialSecurityNumber) } 此验证属性正确呈现data-val-regex和data-val-regex-pattern属性: <input class="SSNMask valid" data-val="true" data-val-regex="Please enter a valid social security number." data-val-regex-pattern="^([0-9]{3}–[0-9]{2}–[0-9]{4})|([?]{3}–[?]{2}–[?]{4})|([0-9]{9,9})$" id="SocialSecurityNumber" name="SocialSecurityNumber" type="text" value="" maxlength="22"> 我无法弄清楚我在AlphaNumeric属性中缺少什么,它没有呈现相应的html属性. 解决方法
我相信您在AlphaNumericAttribute的情况下的问题是您没有为您的字母数字类型的验证器添加JavaScript适配器.
你的代码中肯定有这样的东西: $.validator.unobtrusive.adapters.add('ssn',function(options) { /*...*/ }); 上面的代码声明了SsnAttribute的客户端适配器.请注意,它的名称ssn与ModelClientValidationRule的ValidationType属性中设置的名称相同. 要修复AlphaNumericAttribute的问题,您应该返回ModelClientValidationRegexRule,因为它已经为您的案例(即已经存在的适配器正则表达式)进行了所有必要的设置. [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field,AllowMultiple = false)] public class AlphaNumericAttribute : RegularExpressionAttribute,IClientValidatable { public AlphaNumericAttribute() : base("^[-A-Za-z0-9]+$") { } public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata,ControllerContext context) { yield return new ModelClientValidationRegexRule(FormatErrorMessage(metadata.GetDisplayName()),Pattern); } } 但是如果在正则表达式验证后面的客户端应该有额外的逻辑,你应该编写并注册你自己的不显眼的适配器. 要获得更大的图像,并且为了更好地了解如何在ASP.NET MVC中实现自定义验证,您可以访问Brad Wilson Unobtrusive Client Validation in ASP.NET MVC 3的博客文章,请参阅针对异常验证器的自定义适配器部分. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc-4 – 多个单选按钮组在MVC 4剃刀
- asp.net – 如何根据用户的角色创建具有不同显示
- 如何使用ASP.NET C#设置html输入类型文本值?
- asp.net – 从EF 6 Code First迁移和多个配置生成
- 如何在asp.net MVC3 Razor上编写html字段
- asp.net-mvc-3 – 如何验证列表中的单个项目
- asp.net-mvc – 在ASP.NET MVC中实现自定义配置文
- 定期刷新局部视图(ASP.Net MVC)
- asp.net – 无法加载文件或程序集“Microsoft.Sq
- asp.net-mvc – Visual Studio 2010是否支持MVC