简体   繁体   中英

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.

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:

@{
    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:

[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:

<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.
  • I added 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. It removes the error, but not client validation, duh.
  • I also tried setting the UnobtrusiveJavaScriptEnabled key as false, and keeping the ClientValidationEnabled as true. It also removes the error, but I don't see any client verification. It's always posting back to controller.

Note 1: I get the error while debugging in the view on the line of @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.

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.

To fix this error, while creating the kernel, I added this:

kernel.Unbind<ModelValidatorProvider>();

After adding this, the error was gone and the client side validation was working as intended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM