Viewmodel property could not be found

1.1k Views Asked by At

When submitting my form I'm getting an error that says some property 'Password' I don't have in my viewmodel could not be found.

System.ArgumentException: The property ProjectName.ViewModels.User.UserSecurityViewModel.Password could not be found.

Here is is my html form

<form id="securityForm" class="form-horizontal" method="post" action="/User/SaveSecurityInfo">
  <div class="panel panel-default">
    <div class="panel-heading">Change Your Password</div>
    <div class="panel-body">
      <div class="col-sm-7">
        <div class="form-group">
          <label for="OldPassword" class="col-sm-4 control-label">Old Password</label>
          <div class="col-sm-8">
            <input type="password" name="OldPassword" class="form-control input-lg" id="OldPassword">
          </div>
        </div>
        <div class="form-group">
          <label for="NewPassword" class="col-sm-4 control-label">New Password</label>
          <div class="col-sm-8">
            <input type="password" name="NewPassword" class="form-control input-lg" id="NewPassword">
          </div>
        </div>
        <div class="form-group">
          <label for="ConfirmPassword" class="col-sm-4 control-label">Confirm Password</label>
          <div class="col-sm-8">
            <input type="password" name="ConfirmPassword" class="form-control input-lg" id="ConfirmPassword">
          </div>
        </div>
      </div>
      <div class="col-sm-5">
        some test text @Html.ValidationSummary("", new { @class = "text-danger" })
      </div>
    </div>
    <div class="panel-footer">
      <div class="col-sm-offset-9">
        <button id="updatePassword" type="submit" @*form="securityForm" *@ class="btn btn-primary btn-lg">Update Password</button>
      </div>
    </div>
  </div>
</form>

Here is my viewmodel

 public class UserSecurityViewModel
{
    [Required]
    [StringLength(12, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    public string OldPassword { get; set; }

    [Required]
    [StringLength(12, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    public string NewPassword { get; set; }

    [DataType(DataType.Password)]
    [System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The confirm password does not match, type again!")]
    public string ConfirmPassword { get; set; }
}
2

There are 2 best solutions below

0
On
[DataType(DataType.Password)]
    [System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The confirm password does not match, type again!")]
    public string ConfirmPassword { get; set; }

You probably meant to put NewPassword here if that is what you're comparing to

[DataType(DataType.Password)]
    [System.ComponentModel.DataAnnotations.Compare("NewPassword", ErrorMessage = "The confirm password does not match, type again!")]
    public string ConfirmPassword { get; set; }
0
On

I had the exact same problem, and it was caused by a copy-paste from the register page ViewModel.

The problem is in the Compare DataAnnotation, as the other user pointed out.