简体   繁体   English

如何在客户端将自定义ValidationAttribute呈现为“da​​ta-val-xx”属性?

[英]How can I have a custom ValidationAttribute rendered as a 'data-val-xx' attribute on the client-side?

Given a ViewModel that looks like this: 给定一个如下所示的ViewModel:

public class Login {
    [Required]
    public string Username { get; set; }

    [Required, CustomValidator]
    public string Password { get; set; }
}

And a View like this (Razor syntax here): 像这样的视图(Razor语法在这里):

@Html.TextBoxFor(f => f.Password)

I am getting the following markup: 我得到以下标记:

<input type="text"
       value="" 
       data-val-required="This field is required." />

However I would like it to also include a 'data-' attribute for my custom validator. 但是我希望它还包含我的自定义验证器的'data-'属性。

I want something like this: 我想要这样的东西:

<input type="text" 
       value="" 
       data-val-required="This field is required."
       data-val-customvalidator="XYZ" />

How can I achieve this with ASP.NET MVC 3.0? 如何使用ASP.NET MVC 3.0实现这一目标?

Eg Do I need to put some special attribute on my custom validator? 例如,我是否需要在自定义验证器上添加一些特殊属性? Or register it somewhere? 或者在某处注册?

Well, MSDN saved me (as it often does). 好吧,MSDN救了我(就像它经常那样)。

http://msdn.microsoft.com/en-us/library/ff398048.aspx http://msdn.microsoft.com/en-us/library/ff398048.aspx

So first I have to create an adapter for my validation attribute: 首先,我必须为我的验证属性创建一个适配器:

public class CustomAttributeAdapter : DataAnnotationsModelValidator<EmailAttribute>
{
    public CustomAttributeAdapter(
        ModelMetadata metadata,
        ControllerContext context,
        CustomAttribute attribute) :
        base(metadata, context, attribute)
    {
    }

    public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
    {
        ModelClientValidationRule rule = new ModelClientValidationRule()
        {
            ErrorMessage = ErrorMessage,
            ValidationType = "custom"
        };
        return new ModelClientValidationRule[] { rule };
    }
}

(The 'ValidationType' setting must be lower-case for this to work, as this is the post-fix which will be used as an HTML5 attribute - 'data-val-custom'.) ('ValidationType'设置必须小写才能生效,因为这是将用作HTML5属性的后修复 - 'data-val-custom'。)

Then all I need to do is register it on Application_Start. 然后,我需要做的就是在Application_Start上注册它。

DataAnnotationsModelValidatorProvider.RegisterAdapter(
    typeof(EmailAttribute),
    typeof(EmailAttributeAdapter));

Looking forward to a lot of fun with HTML5 validation. 期待HTML5验证带来很多乐趣。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MVC 3 Custom ValidationAttribute for CheckBoxFor在客户端触发两次 - MVC 3 Custom ValidationAttribute for CheckBoxFor firing twice on client-side 自定义RegularExpressionAttribute缺少用于客户端验证的data-val-regex-pattern - Custom RegularExpressionAttribute missing data-val-regex-pattern for client-side validation 客户端自定义数据注释验证 - Client-side custom data annotation validation 使用自定义[必需]属性时,请使用客户端验证 - Use Client-Side Validation when custom [required] attribute is used 不打扰的规则不会呈现给客户端 - Unobtrusive Rules are not rendered to the client-side 如何在MVC 3中测试自定义ValidationAttribute类 - How can I test custom ValidationAttribute classes in MVC 3 使用ValidationAttribute的自定义验证不会触发客户端验证 - Custom Validation using ValidationAttribute does not fire Client side validation 我可以将客户端验证限制为特定字段吗? - Can I restrict client-side validation to specific fields? 如何在MVC3 / jQuery非侵入式验证中禁止客户端验证消息? - How can I suppress client-side validation messages in MVC3/jQuery unobtrusive validation? ASP.NET MVC3 - 自定义验证属性 - &gt;客户端损坏 - ASP.NET MVC3 - Custom validation attribute -> Client-side broken
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM