本文介绍了在MVC应用程序中永远不会调用GetClientValidationRules的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现IClientValidatable的自定义ValidationAttribute.但是从不调用GetClientValidationRules来将验证规则实际输出到客户端.

该属性没有什么特别之处,但是由于某些原因,它从未被调用.我试过在Application_Start()中注册适配器,但这也不起作用.

  [AttributeUsage(AttributeTargets.Property,AllowMultiple = true)]公共类CustomAttribute:ValidationAttribute,IClientValidatable{公共替代布尔IsValid(对象值){返回true;}#region IClient有效会员公共IEnumerable< ModelClientValidationRule>GetClientValidationRules(ModelMetadata元数据,ControllerContext上下文){字符串errorMessage = FormatErrorMessage(metadata.GetDisplayName());产生返回新的ModelClientValidationRule {ErrorMessage = errorMessage,ValidationType ="custom"};}#endregion}公共类CustomAdapter:DataAnnotationsModelValidator< CustomAttribute>{公共CustomAdapter(ModelMetadata元数据,ControllerContext上下文,CustomAttribute属性):基础(元数据,上下文,属性){}公共重写IEnumerable< ModelClientValidationRule>GetClientValidationRules(){返回this.Attribute.GetClientValidationRules(this.Metadata,this.ControllerContext);}} 

在Application_Start()中,我有:

  DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(CustomAttribute),typeof(CustomAdapter)); 

当我在GetClientValidationRules中放置一个断点时,它永远不会命中.

解决方案

要调用 GetClientValidationRules()方法,您必须启用客户端验证支持.可以通过以下方式完成:

在web.config中(对于应用程序的所有页面):

 < appSettings>< add key ="ClientValidationEnabled" value ="true"/> 

或仅在特定视图上:

两个

  @ {Html.EnableClientValidation();} 

  @(ViewContext.ClientValidationEnabled = true) 

请注意,它必须先移入

  @using(Html.BeginForm()) 

声明.

如果您使用的是 jquery非侵入式验证(当前似乎是标准),则还需要启用它:

 <添加键="UnobtrusiveJavaScriptEnabled" value ="true"/> 

web.config

中的
  @ Html.EnableUnobtrusiveJavaScript() 

查看特定视图.

I have a custom ValidationAttribute that implements IClientValidatable. But the GetClientValidationRules is never called to actually output the validation rules to the client side.

There is nothing special about the attribute but for some reason it is never called. I've tried registering an adapter in Application_Start() but that also doesnt work.

[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class CustomAttribute : ValidationAttribute, IClientValidatable
{
    public override bool IsValid(object value)
    {
        return true;
    }
    #region IClientValidatable Members

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        string errorMessage = FormatErrorMessage(metadata.GetDisplayName());

        yield return new ModelClientValidationRule { ErrorMessage = errorMessage, ValidationType = "custom" };
    }

    #endregion
}

public class CustomAdapter : DataAnnotationsModelValidator<CustomAttribute>
{
    public CustomAdapter(ModelMetadata metadata, ControllerContext context, CustomAttribute attribute)
        : base(metadata, context, attribute)
    {
    }
    public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
    {
        return this.Attribute.GetClientValidationRules(this.Metadata, this.ControllerContext);
    }
}

In Application_Start() I have:

DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(CustomAttribute), typeof(CustomAdapter));

When I put a breakpoint inside GetClientValidationRules it is never hit.

解决方案

In order GetClientValidationRules() method to get called you must enable client-side validation support. It can be done in the following ways:

In the web.config (for all pages of application):

<appSettings>
    <add key="ClientValidationEnabled" value="true" />

Or on particular view only:

either

@{ Html.EnableClientValidation(); }

or

@(ViewContext.ClientValidationEnabled = true)

Please note it must go before

@using (Html.BeginForm())

statement.

If you are using jquery unobtrusive validation (which seems to be a standard currently), you'll also need to enable it:

<add key="UnobtrusiveJavaScriptEnabled" value="true" />

in web.config or

@Html.EnableUnobtrusiveJavaScript()

for particular views.

这篇关于在MVC应用程序中永远不会调用GetClientValidationRules的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 21:45