我爲我的數據庫用戶表創建一個模型類。我的數據庫表沒有一個confirmPassword字段。但我的班上有它。我如何使用MVC比較屬性只插入表格
public class User
{
[Key]
public int id { get; set; }
[Required]
public string password{ get; set; }
[Compare("password", ErrorMessage = "Re-Type password!")]
[NotMapped]
public string confirmPassword { get; set; }
}
我的插入表格已經在html剃刀中有confirmPassword文本框用於比較,因爲我需要它。
<div class="form-group">
@Html.LabelFor(m => m.confirmPassword , new { @class = "control-label" })
@Html.TextBoxFor(m => m.confirmPassword , new { @class = "form-control"})
<h5 class="text-danger">@Html.ValidationMessageFor(m=> m.confirmPassword)</h5>
</div>
但我的更新表單還沒有在html中的confirmPassword文本框。所以當我發佈我的更新表單時,ModelState.IsValid總是爲false。我如何使用比較文本字段只插入表單?因爲我不需要在我的更新表單中使用confirmPassword字段。
謝謝。
for one field ModelState.Remove(「confirmPassword」);對我來說很簡單,謝謝你的回答。 – ASPMaker
嗨,我有同樣的問題,但我不問密碼。我添加ModelState.Remove(「密碼」);和ModelState.Remove(「ConfirmPassword」),但它不起作用。 IsValid是錯的......我缺少什麼?謝謝 – Diego