I have the following (abbreviated) DTO for registering a new user:
[PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage = "The password and confirmation password do not match.")] public class RegisterModel {
Then it is wrapped in a view model as such:
public class RegisterModelViewData: BaseViewData { public RegisterModel RegisterModel { get; set; } public int PasswordLength { get; set; } }
And finally, in the view, I have two fields as such:
<div class="editor-field"> <%= Html.PasswordFor(m => m.RegisterModel.Password) %> <%= Html.ValidationMessageFor(m => m.RegisterModel.Password) %> </div> <div class="editor-field"> <%= Html.PasswordFor(m => m.RegisterModel.ConfirmPassword) %> <%= Html.ValidationMessageFor(m => m.RegisterModel.ConfirmPassword) %> </div>
Apparently, I should get client-side validation and not write if the passwords do not match. I get a message, and then a message saying "Creating a wA account failed", but nothing about the mismatched passwords. I briefly omitted the Required and MininumLength attributes from passwords for brevity, but they seem to behave as expected and check on the client.
source share