【问题标题】:The type or namespace Compare could not be found (are you missing a using directive or an assembly reference?)找不到类型或命名空间比较(您是否缺少 using 指令或程序集引用?)
【发布时间】:2013-08-17 12:16:28
【问题描述】:

我正在尝试向我的网站添加管理员。 在我的 AccountsModels.cs 上发现了问题

它只需要比较实现的数据,但我似乎得到了这个错误。

我也有一个观点:
-Register.cshtml
-LogOn.cshtml
-ChangePasswordSuccess.cshtml
-ChangePassword.cshtml

当然还有一个 AccountController.cs..

有人知道解决方案吗?

代码如下:

using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using System.Web.Security;

namespace Videoteek.Domain.Models
{
    public class ChangePasswordModel
    {
        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Current password")]
        public string OldPassword { get; set; }

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

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

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

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

        [Display(Name = "Remember me?")]
        public bool RememberMe { get; set; }
    }

    public class RegisterModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { 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")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }

        //****
        [Required]
        [Display(Name = "Security Question")]
        public string PwdQuestion { get; set; }
        [Required]
        [Display(Name = "Security Answer")]
        public string PwdAnswer { get; set; }

    }
}

【问题讨论】:

  • 你的问题的标题就是你的答案

标签: c# asp.net-mvc


【解决方案1】:

从您的代码看来,您希望将密码与确认密码进行比较。如果是这样,那么你的属性

Compare

不正确。应该是

[CompareAttribute("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]

您已经在代码中添加了所需的命名空间。你可以阅读更多关于它的信息here

【讨论】:

  • 这行不通。 “比较”属性和“比较属性”都不起作用
【解决方案2】:

我也有同样的错误。我意识到,当我使用 .net 4.0 时,System.Web.Mvc;System.ComponentModel.DataAnnotations; 使用它已解决。但是当我将框架更改为 4.5 时,错误是

'CompareAttribute' is an ambiguous reference between 'System.ComponentModel.DataAnnotations.CompareAttribute' and 'System.Web.Mvc.CompareAttribute'

The type or namespace name 'Compare' could not be found (are you missing a using directive or an assembly reference?)

结论 如果框架是 4.0

System.Web.Mvc;

System.ComponentModel.DataAnnotations;

并重建项目 否则,如果框架是 4.0

System.ComponentModel.DataAnnotations;

并重建项目

不要忘记始终以管理员身份(模式)运行 Visual Studio

【讨论】:

  • 这就是发生在我身上的事情。但是在framework 4.0版本中,去掉对System.Web.Mvc的引用并不能解决问题。
【解决方案3】:

我遇到此错误的情况是我试图从 4.0 升级到 .Net 框架 4.5。这不起作用,我恢复到 4.0 并收到此错误。

我通过输入修复了这个构建错误

[System.Web.Mvc.Compare]

【讨论】:

    【解决方案4】:

    .NET 中有 2 个属性 可能位于程序集System.Web.Mvc.dll 中的命名空间System.Web.Mvc.CompareAttribute 中的那个可以完成这项工作。

    还有另一个(重复的)做同样的事情。完整路径是程序集System.ComponentModel.DataAnnotations.dll 中的System.ComponentModel.DataAnnotations.CompareAttribute,正如我所见,这是您引用的路径。那么您可能需要引用 DLL System.ComponentModel.DataAnnotations.dll

    您可以检查您是否没有同时引用两者,因为这也可能导致问题。不过,这似乎不是问题所在,我只是提一下。

    【讨论】:

      【解决方案5】:

      如果你使用的是Web API类,去掉“using System.Web.Mvc;”并更改为“使用 System.Web.Http;”并保持“比较”关键字不变。但是如果你使用的是纯 MVC,请将“Compare”关键字更改为“CompareAttribute”。

      System.ComponentModel.DataAnnotations;这两种情况都需要进行验证。但是“比较”或“比较属性”并不是纯粹用于验证。它是系统基于其架构模式(MVC 或 RestFul Web API)提供的一种工具。它使用属性类来简化引用和使用“比较工具”

      【讨论】:

        【解决方案6】:

        要在 .NET4 和 .NET45 之间进行这项工作,您需要将这些文件中的两个 using 语句更改为:

        using System.ComponentModel.DataAnnotations;
        using CompareAttribute = System.Web.Mvc.CompareAttribute;
        

        【讨论】:

          【解决方案7】:

          添加对 System.Web.Mvc.dll 程序集的引用!

          【讨论】:

            【解决方案8】:

            简单,以命名空间为前缀:

            System.ComponentModel.DataAnnotations.Compare
            

            由于所有其他属性(如 [Required]、[Display] 等)都使用此命名空间,因此这是有道理的。

            编译器会感到困惑,因为 System.Web.Mvc 命名空间也有一个名为“Compare”的方法,因此 Framework 4.5 希望您明确并消除歧义。如果将光标悬停在其他属性上,您将看到它们使用 System.ComponentModel.DataAnnotations 命名空间,但不需要限定,因为它们与其他命名空间不冲突,而 [Compare] 存在于两个命名空间中。有趣的是,框架 4 是如何工作的,但框架 4.5 需要被告知......

            【讨论】:

              猜你喜欢
              • 2011-02-12
              • 1970-01-01
              • 1970-01-01
              • 2013-08-28
              • 1970-01-01
              • 1970-01-01
              • 2012-05-01
              • 2020-11-28
              • 2015-06-12
              相关资源
              最近更新 更多