Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have this error in my AccountController .

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

The obvious fix is to add using System.Web.Mvc; However when I do I get 4 new errors

On two difference lines:

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

On another 2 different lines:

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

Why does this happen and how do I fix it?

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; }


}

ResetPasswordViewModel

public class ResetPasswordViewModel
{

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

}
like image 791
TykiMikk Avatar asked Mar 30 '16 19:03

TykiMikk


1 Answers

Yea. Both of those namespaces has that attribute which has same functionality.

As per the msdn documentation, System.Web.Mvc.CompareAttribute is obsolete and it is recommended to use System.ComponentModel.DataAnnotations.CompareAttribute

So either use the fully qualified name including the namespace.

[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; }

Or you can use an alias name if you do not want to put the fully qualified name in all the places

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
}
like image 167
Shyju Avatar answered Sep 30 '22 07:09

Shyju