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

Tyk*_*ikk 11 c# asp.net-mvc entity-framework asp.net-identity

我的AccountController中有这个错误.

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

显而易见的解决方法是添加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<SelectListItem> DepotList { get; set; }


}
Run Code Online (Sandbox Code Playgroud)

ResetPasswordViewModel

public class ResetPasswordViewModel
{

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

}
Run Code Online (Sandbox Code Playgroud)

Shy*_*yju 25

是啊.这两个名称空间都具有相同功能的属性.

根据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; }
Run Code Online (Sandbox Code Playgroud)

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

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
}
Run Code Online (Sandbox Code Playgroud)