简体   繁体   English

在 ASP.NET MVC 5 中出现以下错误:不显眼的客户端验证规则中的验证类型名称必须是唯一的。 所需

[英]Getting the following error in ASP.NET MVC 5: Validation type names in unobtrusive client validation rules must be unique. The […] required

I am working with ASP.NET MVC 5 and trying to use the [Required] attribute to enable client side verification.我正在使用 ASP.NET MVC 5 并尝试使用[Required]属性来启用客户端验证。

I get the following error:我收到以下错误:

Validation type names in unobtrusive client validation rules must be unique.不显眼的客户端验证规则中的验证类型名称必须是唯一的。 The following validation type was seen more than once: required多次看到以下验证类型:必需

I've searched for this error around here, but anything I tried didn't really help.我在这里搜索过这个错误,但我尝试的任何东西都没有真正帮助。

Index.cshtml view: Index.cshtml 视图:

@{
    ViewBag.Title = "Login";
}

@model CapitalWorkflowWebApplication.Models.LoginModel

<h2>Login</h2>

@using (Html.BeginForm("login-user", "Login", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    <div class="container">
        <div class="row">
            Login
        </div>
        <div class="row">
            @Html.LabelFor(model => model.Username)
            @Html.TextBoxFor(model => model.Username)
            @Html.ValidationMessageFor(model => model.Username, "", new { @class = "text-danger" })
        </div>
        <div class="row">
            @Html.LabelFor(model => model.Password)
            @Html.TextBoxFor(model => model.Password)
            @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
        </div>
        <div class="row">
            <input type="submit" value="Login" />
        </div>
    </div>
}

LoginModel:登录型号:

using System.ComponentModel.DataAnnotations;

namespace CapitalWorkflowWebApplication.Models
{
    public class LoginModel
    {
        [Required(ErrorMessage = "Le champ utilisateur est obligatoire")]
        [Display(Name = "Utilisateur")]
        public string Username { get; set; }

        [Required(ErrorMessage = "Le champ mot de passe est obligatoire")]
        [Display(Name = "Mot De Passe")]
        [DataType(DataType.Password)]
        public string Password { get; set; }

        public string ErrorMessage { get; set; }
    }
}

Following some of the comments/questions/answers here, I tried to set a breakpoint in my controller, and in the immediate window I ran this: ModelValidatorProviders.Providers and the result was:在此处的一些评论/问题/答案之后,我尝试在我的 controller 中设置断点,并在立即 window 中运行: ModelValidatorProviders.Providers ,结果是:

[0]: {System.Web.Mvc.DataAnnotationsModelValidatorProvider}
[1]: {System.Web.Mvc.DataErrorInfoModelValidatorProvider}
[2]: {System.Web.Mvc.ClientDataTypeModelValidatorProvider}

I haven't used any custom validators.我没有使用任何自定义验证器。

In my web.config , I have these settings:在我的web.config中,我有以下设置:

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

and in my layout I do have:在我的布局中,我确实有:

<script src="@Url.Content("~/Scripts/jquery-3.3.1.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

What I tried so far:到目前为止我尝试了什么:

  • I removed the [Required] attribute from the LoginModel which removes the error, but also remove client verification, which is not the desired result.我从LoginModel中删除了 [Required] 属性,该属性删除了错误,但也删除了客户端验证,这不是预期的结果。
  • I added DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;我添加了DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false; to my application start code, but the error is always there.到我的应用程序启动代码,但错误始终存在。
  • I tried setting the ClientValidationEnabled key in web.config to false, but keeping the UnobtrusiveJavaScriptEnabled as true.我尝试将 web.config 中的ClientValidationEnabled键设置为 false,但保持UnobtrusiveJavaScriptEnabled为 true。 It removes the error, but not client validation, duh.它消除了错误,但没有消除客户端验证,呵呵。
  • I also tried setting the UnobtrusiveJavaScriptEnabled key as false, and keeping the ClientValidationEnabled as true.我还尝试将UnobtrusiveJavaScriptEnabled键设置为 false,并将ClientValidationEnabled保持为 true。 It also removes the error, but I don't see any client verification.它也消除了错误,但我没有看到任何客户端验证。 It's always posting back to controller.它总是发回 controller。

Note 1: I get the error while debugging in the view on the line of @Html.TextBoxFor(model=>model.Username);注1:我在@Html.TextBoxFor(model=>model.Username);行的视图中调试时出现错误。

Note 2: I am using Ninject as a Dependency resolver, if needed, I'll post my ninject config class, since I've seen some comments saying it could be the DI container causing this problem.注意 2:我使用 Ninject 作为依赖关系解析器,如果需要,我将发布我的 ninject 配置 class,因为我看到一些评论说它可能是导致此问题的 DI 容器。

What could possibly be the problem I am facing, and how to solve it?我可能面临的问题是什么,以及如何解决?

After some more digging and specially searching for problems related to Ninject, it turned out that Ninject was actually the source of the problem.经过一番挖掘和专门搜索与Ninject相关的问题,结果发现Ninject实际上是问题的根源。

To fix this error, while creating the kernel, I added this:为了修复这个错误,在创建 kernel 时,我添加了这个:

kernel.Unbind<ModelValidatorProvider>();

After adding this, the error was gone and the client side validation was working as intended.添加后,错误消失了,客户端验证按预期工作。

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

相关问题 不打扰的客户端验证规则中的验证类型名称必须唯一。 多次看到以下验证类型:必需 - Validation type names in unobtrusive client validation rules must be unique. The following validation type was seen more than once: required 不打扰的客户端验证规则必须唯一 - unobtrusive client validation rules must be unique 不引人注目的ASP.NET MVC自定义验证 - ASP.NET MVC Custom Validation with Unobtrusive ASP.NET MVC3验证类型名称错误 - ASP.NET MVC3 Validation Type Names Error 使用fluentvalidation和asp.net mvc LessThanOrEqualTo不触发的不显眼的客户端验证 - unobtrusive client validation using fluentvalidation and asp.net mvc LessThanOrEqualTo not firing 在 ASP.NET Core MVC 中不工作的 bool 字段的不显眼的客户端验证 - Unobtrusive client-side validation for bool field not working in ASP.NET Core MVC ASP.NET MVC4不显眼的验证本地化 - ASP.NET MVC4 Unobtrusive validation localization ASP.NET MVC 4.0中非侵入式验证中的奇怪行为 - Strange behavior in Unobtrusive validation in ASP.NET MVC 4.0 ASP.NET MVC 验证错误:<field> 字段为必填项 - ASP.NET MVC Validation error: The <field> field is required 使用jQuery的ASP.Net 2012不干扰验证 - ASP.Net 2012 Unobtrusive Validation with jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM