2013-08-17 103 views
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

回答

5

从您的代码看起来,您想要将您的密码与确认密码进行比较。如果是这样的话你的属性

Compare 

是不正确的。它应该是

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

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

+0

谢谢!这工作 – Pex

0

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

+1

他已经有那 – Ehsan

1

有2个属性在.NET 大概是一个坐落在装配System.Web.Mvc.dll将做的工作的命名空间System.Web.Mvc.CompareAttribute

还有一个(重复)它做同样的事情。完整的路径是System.ComponentModel.DataAnnotations.CompareAttribute,汇编System.ComponentModel.DataAnnotations.dll这是你所引用的,我可以看到。然后,你可能会需要的DLL System.ComponentModel.DataAnnotations.dll

你可以检查你有没有同时参照基准,因为这也可能会出现问题。似乎这不是问题,但我只是提到。

1

我也有同样的错误。我意识到,当我使用.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为(中)管理员(模式)

+0

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

2

我在收到此错误的情况是,我试图升级到.Net框架4.5从4.0开始。这没有奏效,我恢复到4.0,并得到这个错误。

我通过把在

[System.Web.Mvc.Compare]

1

如果您正在使用的Web API的类,采取了“使用System.Web.Mvc固定此生成错误; “并更改为“使用System.Web.Http;”并保留“比较”关键字。但是,如果您使用的是纯MVC,请将“Compare”关键字更改为“CompareAttribute”。

System.ComponentModel.DataAnnotations;这两种情况都需要验证。但“比较”或“比较属性”纯粹不用于验证。它是由系统根据其架构模式(MVC或RestFul Web API)提供的工具。它使用属性类,简化参考和使用“比较设施”

0

简单,前缀与命名空间中:

System.ComponentModel.DataAnnotations.Compare 

像[必填],[显示]等所有其他属性都使用这个命名空间,然后它是有道理的。

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

0

为了.NET4之间这项工作.NET45你需要你的两个using语句在这些文件更改为:

using System.ComponentModel.DataAnnotations; 
using CompareAttribute = System.Web.Mvc.CompareAttribute; 
相关问题