找不到Viewmodel属性

时间:2017-08-31 17:41:52

标签: c# asp.net-mvc asp.net-mvc-viewmodel

提交表单时,我收到一条错误消息,指出找不到我在viewmodel中没有的“密码”属性。

  

System.ArgumentException:找不到属性ProjectName.ViewModels.User.UserSecurityViewModel.Password。

这是我的html表单

<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>

这是我的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 个答案:

答案 0 :(得分:4)

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

如果你正在与

进行比较,那么你可能想把NewPassword放在这里
[DataType(DataType.Password)]
    [System.ComponentModel.DataAnnotations.Compare("NewPassword", ErrorMessage = "The confirm password does not match, type again!")]
    public string ConfirmPassword { get; set; }

答案 1 :(得分:0)

我遇到了完全相同的问题,这是由注册页面ViewModel中的复制粘贴引起的。

问题出在另一个用户指出的“比较数据注释”中。