1
我有一個非常簡單的應用程序。這是我的視圖代碼;ModelState無法正常工作
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Kategori Modeli</legend>
<div class="editor-label">
Category Name
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CategoryName)
@Html.ValidationMessageFor(model => model.CategoryName)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>}
這是我的模型代碼;
public int ID { get; set; }
[Required(ErrorMessage = "Category Name cannot be null.")]
[Range(3, 25, ErrorMessage = "Category Name must have 3-25 characters")]
public string CategoryName { get; set; }
並且插入代碼;
[HttpPost, ValidateInput(false)]
public ActionResult Insert(Category _category)
{
if (ModelState.IsValid)
{
---
}
}
即使它有4個字符,ModelState.IsValid始終爲false。請幫幫我。它總是顯示「類別名稱必須有3-25個字符」的錯誤信息。