Reputation: 1336
I am developing an MVC 5 application and using the built in AccountController methods for User Registrations. I have added a User Name property in RegisterViewModel since I needed that. Now I want to add Remote Validation on User Name property to prompt the user immediately if the user name is repeating.
But when I tried Remote Validation and added System.Web.Mvc reference, the Compare attribute for Password and Confirm Password started giving error.
I went through some study online and got that System.Web.Mvc has also got a Compare method and adding a reference to this class confused the compiler about Compare method.
My RegisterViewModel is:
public class RegisterViewModel
{
[Required]
[EmailAddress]
[Display(Name = "Email")]
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 = "User Name")]
[RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed.")]
[Remote("DuplicateUserName", "Account", ErrorMessage = "UserName already exists.")]
public string UserName { get; set; }
}
The error it gave is:
'CompareAttribute' is an ambiguous reference between 'System.ComponentModel.DataAnnotations.CompareAttribute' and 'System.Web.Mvc.CompareAttribute'
Now I cannot delete the reference to using System.ComponentModel.DataAnnotations class because I am using Required and Display attributes of that class. But I also want to add Remote Validation. How can I do it?
Upvotes: 2
Views: 1024
Reputation: 66
Add the full namespace to the attrubute
[System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
Alternatively you can add an alias to your usings
using Remote = System.Web.Mvc.RemoteAttribute;
Upvotes: 2
Reputation: 11597
You can give the full path to the attribute, for instance:
[System.Web.Mvc.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
Upvotes: 2