jquery - 流畅的验证。错误: Validation type names in unobtrusive client validation rules must be unique

标签 jquery c#-4.0 asp.net-mvc-3 fluentvalidation

我收到错误:

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

我使用了服务器验证。一切都很好。 但现在我声明使用客户端验证,但遇到了这个问题。

这是我的验证类代码:

public class TestViewDataValidation : BaseTestCreateViewDataValidation<BaseTestCreateViewData>
    {

public TestViewDataValidation ()
        {
            this.RuleFor(x => x.Login).NotNull();
            this.RuleFor(x => x.Login).NotEmpty();
            this.RuleFor(x => x.Login).EmailAddress();          
        }
}

但是如果我留下一个验证器 - 一切都会正常。 我应该做什么才能对字段进行不止一次验证。

最佳答案

如果您对同一元素多次进行相同的验证,则会显示此错误。

不为默认 DataAnnontations 设置 AddImplicitRequiredAttributeForValueTypes = false ,您的 FluentValidation 将为任何 ValueType(如 int)添加必需的验证。如果您同时在任何 ValueType 上添加 RuleFor(或 [Required] 属性),您将为该字段添加一个额外的 required。

出于这个原因(我想显式设置所有验证),我的 Application_Start() 中有以下内容:

var fluentValidationModelValidatorProvider = new FluentValidationModelValidatorProvider(new AttributedValidatorFactory());
ModelValidatorProviders.Providers.Add(fluentValidationModelValidatorProvider);
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
fluentValidationModelValidatorProvider.AddImplicitRequiredValidator = false;

关于jquery - 流畅的验证。错误: Validation type names in unobtrusive client validation rules must be unique,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4767457/

相关文章:

javascript - 获取 div 中 anchor 的 href 并将其应用到图像?

javascript - 如果功能激活不止一次,则多次停止输出打印

jquery - 在页面加载时为每个 Div 运行 jQuery 脚本

c#-4.0 - ASP.NET MVC4所有领域的列表

c#-4.0 - Windows 8 - 等待任务 <Bool> - 需要完成监听器的异步回调

asp.net-mvc - 带有特殊字符的 MVC 路由

c# - 如何在 C# 4.0 中读取内容类型为 application/json 的 HTTP Post 数据

javascript - Ajax 不仅返回特定 div 的数据,还返回完整的 html 结构

c# - Activator.CreateInstance 和 Ninject

asp.net-mvc-3 - ASP.NET MVC 3 : Append to sections