使用基于模型的验证并让您查看看起来像这样
@model SampleApplication.Models.BasicDemoModel
<form id="AjaxForm" action="/">
<table>
<tr>
<td>@Html.LabelFor(x => x.Name)</td>
<td>
@Html.TextBoxFor(x => x.Name)
@Html.ValidationMessageFor(x => x.Name, "*")
</td>
</tr>
<tr>
<td>@Html.LabelFor(x => x.Email)</td>
<td>
@Html.TextBoxFor(x => x.Email)
@Html.ValidationMessageFor(x => x.Email, "*")
</td>
</tr>
@{
Html.RenderPartial("Address", Model);
}
</table>
@if (!string.IsNullOrWhiteSpace(Model.Message))
{
<h2>@Model.Message</h2>
}
@if (!ViewContext.ViewData.ModelState.IsValid)
{
@Html.ValidationSummary()
}
<input type="submit" title="Submit Form" onclick="PostFormWithAjax();return false;" />
</form>
如果您需要额外的验证消息,您可以像这样将其添加到更多状态
public ActionResult ChangeRiskCategory(Guid id)
{
//...
//call server side method, handle errors
//...
ModelState.AddModelError("MyInput","This isn't Right");
return View("ChangeRiskCategory", changeRiskCategoryModel);
}