‘Compare’是’System.ComponentModel.DataAnnotations.CompareAttribute’和’System.Web.Mvc.CompareAttribute’之间的模糊引用。

我的AccountController中有这个错误。

找不到类型或命名空间名称’SelectListItem’(您是否缺少using指令或程序集引用?

显而易见的解决方法是using System.Web.Mvc;添加using System.Web.Mvc; 但是,当我这样做时,我得到4个新错误

在两个不同的行:

找不到类型或命名空间名称’ErrorMessage’(您是否缺少using指令或程序集引用?)

在另外两个不同的行:

‘Compare’是’System.ComponentModel.DataAnnotations.CompareAttribute’和’System.Web.Mvc.CompareAttribute’之间的模糊引用。

为什么会发生这种情况,我该如何解决?

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

ResetPasswordViewModel

 public class ResetPasswordViewModel { [DataType(DataType.Password)] [Display(Name = "Confirm password")] [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] } 

是啊。 这两个名称空间都具有相同function的属性。

根据msdn文档 , System.Web.Mvc.CompareAttribute已过时,建议使用System.ComponentModel.DataAnnotations.CompareAttribute

因此要么使用包含命名空间的完全限定名称。

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

或者,如果您不想在所有位置放置完全限定名称,则可以使用别名

 using Compare = System.ComponentModel.DataAnnotations.CompareAttribute; public class ResetPasswordViewModel { [DataType(DataType.Password)] [Compare("Password", ErrorMessage = "The password and confirm password do not match.")] public string Password { set;get;} //Other properties as needed }