I use the following model to control user input and to register him.
But if I input the same passwords and press a button the message about that confirmation password is wrong appears.
I am sure about the model is correct because it works in some other page.
In this case RegisterModelSecond is a member of EventFrontEndViewModel.
So when I comment [Compare("RegisterModel.Password", ErrorMessage = "The password and confirmation password do not match.")] it works but no confirmation I need!
Any clue how to fix it?
public class EventFrontEndViewModel
{
public Page CurrentPage { set; get; }
public List<Event> Events { set; get; }
public List<Event> SubscribedEvents { set; get; }
public RegisterModelSecond RegisterModel { set; get; }
public EventFrontEndViewModel()
{
CurrentPage = new Page();
Events = new List<Event>();
RegisterModel = new RegisterModelSecond();
SubscribedEvents = new List<Event>();
}
}
Register Model
public class RegisterModelSecond
{
[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; }
[Required]
[DataType(DataType.Password)]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
and HTML
<div class="editor-label">
@Html.LabelFor(m => m.RegisterModel.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.RegisterModel.Password)
@Html.ValidationMessageFor(m => m.RegisterModel.Password)
</div>
<div class="clear">
</div>
<div class="editor-label">
@Html.LabelFor(m => m.RegisterModel.ConfirmPassword)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.RegisterModel.ConfirmPassword)
@Html.ValidationMessageFor(m => m.RegisterModel.ConfirmPassword)
</div>
<div class="clear">
</div>
I found correct answer here MVC 3 Client-Side Compare Validation
This is a bug in the client side validation script: jquery.validate.unobtrusive.js
At Line ~284 you’ll find this:
Change it to this:
The name attribute needs single quotes.
I want to add if you use MIN version u have to change it there as well.