【问题标题】:ASP.NET MVC3 Validation Type Names ErrorASP.NET MVC3 验证类型名称错误
【发布时间】:2013-07-30 15:30:46
【问题描述】:

我收到以下错误:

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

我不知道为什么,我已经阅读了一些关于自定义验证和 ninject 的内容,但我不认为是这样。

我正在创建一个自定义帐户管理系统,管理员可以在其中创建/编辑用户。我正在使用ASP.NET Membership, Role, Profile。当您创建一个带有Internet 的新应用程序时,它会创建所有帐户内容。我所做的只是重用RegisterModel 中提供的AccountManagement Area。但随后错误开始出现,我没有任何自定义验证提供程序或任何东西。错误/异常也出现在 Account Views 中(由 MVC 模板创建的)。

错误发生在视图的这一行:

<div class="editor-label">
    @Html.LabelFor(model => model.User.UserName)
</div>
<div class="editor-field">
    @Html.TextBoxFor(model => model.User.UserName) <!-- THIS LINE -->
    @Html.ValidationMessageFor(model => model.User.UserName)
</div>

我的模型是一个带有属性的ViewModel 类:

public RegisterModel User {get;set;}

除了重用RegisterModel 之外,我已经编写了整个网站,我做任何事情的方式都没有问题。从那以后,我尝试创建一个全新的Model,称为NewUserModel,并仅在Area 中引用它,但无济于事。

我使用DependencyResolver 来使用Ninject 作为我的IoC/DI。我无法想象这是一个问题......

型号:

namespace MyApplication.Areas.AccountManagement.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Web;

    public class NewUserModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }

        [Required]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }

        [Required, RegularExpression(@"[0-9]{8}", ErrorMessage = "Please enter in an 8 digit Intel Worldwide Id")]
        [Display(Name = "WWID")]
        public string WWID { get; set; }

        [Required]
        [DataType(DataType.EmailAddress)]
        [Display(Name = "Email address")]
        public string Email { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [System.Web.Mvc.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }
}

【问题讨论】:

  • 您是否以某种方式多次包含jquery.validate.unobtrusive.js
  • RegisterModel 是什么样的?你能用 EditorFor 代替 TextBoxFor 吗?
  • 错误是异常,还是出现在控制台中还是...?
  • @Ic。我不这么认为,我检查了View,它只有 1 个参考。就像我说的,我以前用同样的方式创建过一百次这样的视图!

标签: c# jquery asp.net-mvc-3 unobtrusive-validation


【解决方案1】:

如果对属性应用了多个验证规则,通常会出现此类问题。 您能检查以下内容吗:

  1. 如果有任何其他自定义验证器正在验证模型,请查找并检查对 NewUserModel 的所有引用。
  2. 检查您的 NewUserModel 继承/实现了什么

【讨论】:

  • 它没有使用任何自定义验证器,也没有继承或实现任何其他
【解决方案2】:

由于将 Ninject.MVC3 从 nuget 添加到项目中而出现此错误。

这与 Ninject 如何注入验证有关,或者这是我可以在网上找到的最佳答案!

我从我的应用程序中完全删除了 Ninject,然后重新添加了 Ninject(正常的),一切正常!!

【讨论】:

    猜你喜欢
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 2014-11-21
    • 2011-08-08
    • 1970-01-01
    • 2012-03-04
    相关资源
    最近更新 更多