2017-08-31 110 views
0

當提交我的表單時,我收到一個錯誤,說我的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>

這裏是我的視圖模型

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; } 
} 
+0

你沒有認真生成你的html手動嗎?使用'HtmlHelper'方法(例如'Html.PasswordFor(m => m.OldPassword,new {@class =「...」})'爲客戶端驗證生成'data-val- *'屬性並且擁有2路模型綁定。 –

回答

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

你可能意味着把NEWPASSWORD這裏,如果那是什麼你是compari ng到

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