在不显眼的客户端验证规则验证类型名称必须是唯一的 [英] Validation type names in unobtrusive client validation rules must be unique

查看:277
本文介绍了在不显眼的客户端验证规则验证类型名称必须是唯一的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在不显眼的客户端验证规则验证类型名称必须是
  独特。下面的验证类型被视为不止一次:
  需要

Validation type names in unobtrusive client validation rules must be unique. The following validation type was seen more than once: required

这是指EmailAddress的财产,在这里:

This is referring to the EmailAddress property, here:

public class LoginModel
{
    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email")]
    [AdditionalMetadata("Style", "Wide")]
    public string EmailAddress { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    [AdditionalMetadata("Style", "Wide")]
    public string Password { get; set; }
}

我不使用同一类型的验证规则在这里两次。当部署到服务器能正常工作在本地,但不是。这是怎么回事?

I'm not using the same type of validation rule twice here. This works fine locally, but not when deployed to the server. What's the deal?

我做了一个引用添加到 DataAnnotationExtensions http://dataannotationsextensions.org ),这可能会造成一个问题?

I did add a reference to DataAnnotationExtensions (http://dataannotationsextensions.org), could that be causing an issue?

编辑:删除该引用并没有解决问题。看来可能有些内容与IIS配置被搞砸了?

removing the reference did not fix the problem. It seems something may be messed up with the IIS configuration?

推荐答案

<一个href=\"http://stackoverflow.com/questions/9746186/validation-type-names-in-unobtrusive-client-validation-rules-must-be-unique#comment26469383_9746186\">JimmiTh's上提供了一个重要观点的问题发表评论为我解决这个自己。

在我而言,我绝对没有额外的提供程序添加到 ModelValidatorProviders 。我添加了一个自定义的验证工厂(使用流利验证)在我的的Global.asax.cs此code 的文件:

In my case, I definitely did add an additional provider to ModelValidatorProviders. I added a custom validation factory (using Fluent Validation) with this code in my Global.asax.cs file:

ModelValidatorProviders.Providers.Add(
    new FluentValidationModelValidatorProvider(validatorFactory));

但是,使用多个供应商是不是的不一定的问题。似乎是什么问题是,如果多个供应商提供同样的验证,因为这会多次注册相同的规则,与Microsoft不显眼的审定code造成上述问题。

But using multiple providers isn't necessarily problematic. What seems to be problematic is if multiple providers provide the same validators, because that will register the same rules multiple times, causing the mentioned problem with the Microsoft unobtrusive validation code.

我结束了在同一个文件删除以下行,我决定我并不需要同时使用供应商:

I ended up removing the following line from the same file as I decided I didn't need to use both providers:

FluentValidationModelValidatorProvider.Configure();

上面的配置方法本身添加提供商 ModelValidatorProviders ,我被有效注册相同的验证器类两次,因此对非唯一的验证类型名称的错误。

The Configure method above is itself adding a provider to ModelValidatorProviders, and I was effectively registering the same validator class twice, hence the error about non-unique "validation type names".

所谓问题<一href=\"http://stackoverflow.com/questions/4767457/fluent-validations-error-validation-type-names-in-unobtrusive-client-validatio\">jquery - 流利的验证。错误:在不显眼的客户端验证规则验证类型名必须是唯一的点到另一个的方式,使用多个供应商可以导致所提到的问题。每个供应商可以配置为一个隐必需属性为增值型(即不可为空即视图模型属性)。要解决这个问题上,我可以我的code更改为以下,这样没有供应商添加隐必需的属性:

The SO question jquery - Fluent Validations. Error: Validation type names in unobtrusive client validation rules must be unique points to another way that using multiple providers can lead to the mentioned problem. Each provider can be configured to add an 'implicit required attribute to 'value types' (i.e. view model properties that aren't nullable). To resolve this particular issue, I could change my code to the following so that none of the providers add implicit required attributes:

FluentValidationModelValidatorProvider.Configure(
    provider => provider.AddImplicitRequiredValidator = false);


DependencyResolverValidatorFactory validatorFactory =
    new DependencyResolverValidatorFactory();

FluentValidationModelValidatorProvider validatorFactoryProvider =
    new FluentValidationModelValidatorProvider(validatorFactory);

validatorFactoryProvider.AddImplicitRequiredValidator = false;
ModelValidatorProviders.Providers.Add(validatorFactoryProvider);


DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false; 

这篇关于在不显眼的客户端验证规则验证类型名称必须是唯一的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆