[英]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:到目前为止我尝试了什么:
LoginModel
which removes the error, but also remove client verification, which is not the desired result.LoginModel
中删除了 [Required] 属性,该属性删除了错误,但也删除了客户端验证,这不是预期的结果。DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
to my application start code, but the error is always there.ClientValidationEnabled
key in web.config to false, but keeping the UnobtrusiveJavaScriptEnabled
as true.ClientValidationEnabled
键设置为 false,但保持UnobtrusiveJavaScriptEnabled
为 true。 It removes the error, but not client validation, duh.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. 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.