类型或命名空间“Compare”无法找到(是否缺少using指令或程序集引用?)

5
我想在我的网站上添加管理员。问题出现在我的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; }

    }
}

2
你的问题标题就是你的答案。 - Rohit
8个回答

5

根据你的代码,看起来你想要将密码和确认密码进行比较。如果是这样的话,那么你的属性

Compare

不正确。应该为

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

您已经在代码中添加了所需的命名空间。您可以在此处了解更多信息。


这个不起作用。无论是“Compare”属性还是“CompareAttribute”都不起作用。 - Heemanshu Bhalla

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


这就是发生在我身上的事情。但在4.0版本的框架中,删除对System.Web.Mvc的引用并不能解决问题。 - arame3333

2

我遇到这个错误的情况是因为我尝试从4.0升级到.NET Framework 4.5。这并没有成功,然后我回滚到了4.0版本,结果出现了这个错误。

我通过添加[System.Web.Mvc.Compare]来解决这个构建错误。


1
如果您正在使用Web API类,则将“using System.Web.Mvc;”移除,并改为“using System.Web.Http;”,保留“Compare”关键字不变。但是,如果您仅使用MVC,则将“Compare”关键字更改为“CompareAttribute”。
无论哪种情况,都需要System.ComponentModel.DataAnnotations;进行验证。但是,“Compare”或“CompareAttribute”并非仅用于验证。它是系统基于其架构模式(MVC或RestFul Web API)提供的一种工具。它使用属性类来简化引用和使用“Compare facility”。

1
在.NET中有两个属性。可能位于命名空间System.Web.Mvc.CompareAttribute的那个位于程序集System.Web.Mvc.dll中的属性会完成任务。
还有另一个(重复的),它做同样的事情。完整路径是System.ComponentModel.DataAnnotations.CompareAttribute,位于程序集System.ComponentModel.DataAnnotations.dll中,这是您正在引用的那个。然后您可能需要引用System.ComponentModel.DataAnnotations.dll DLL。
您可以检查是否没有引用这两个DLL,因为这也可能导致问题。尽管如此,我还是提一下。

1

为了使.NET4和.NET45之间可以工作,您需要更改这些文件中的两个使用语句:

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

0

简单地,使用命名空间前缀:

System.ComponentModel.DataAnnotations.Compare

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

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


0

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接