Tuesday, 6 August 2013

ModelState.IsValid is false after Jquery Validation is true

ModelState.IsValid is false after Jquery Validation is true

I am trying to create a simple create user page that contains a users
details and a confirm password field.
I have a User view model and a User class:
public class UserViewModel
{
public User AccountUser { get; set; }
[Required]
[Compare("AccountUser.Password")]
public string ConfirmPassword { get; set; }
}
[DisplayName("User Account")]
public class User
{
[Required]
[DisplayName("Username")]
public string Username { get; set; }
[Required]
[DisplayName("Password")]
public string Password { get; set; }
}
When entering in mismatched data into the two password fields, the jQuery
will successfully tell me that the password fields to not match and the
submit button will not fire back to my controller.
However if I match the two fields, the jQuery will pass its validation and
the submit query will fire back to the controller. At that point my
ModelState.IsValid is checked and it returns false citing "Could not find
property AccountUser.Password" on the errors for the Password property.
I understand the separation of concerns pattern, however I am trying to
understand why this code does not work.
Can someone explain why the modelstate is returning false after no jquery
validation messages are displayed?

No comments:

Post a Comment