我正在尝试使用 Ajax.BeginForm 将数据发布到控制器。在出现特定错误的情况下,表单应重新呈现并显示添加到 ModelState 的自定义错误消息。由于某种原因,错误消息未显示。我什至正在尝试以下不起作用的测试用例,我错过了什么吗?
Edit.cshtml:
@using (Ajax.BeginForm("Edit", "UserInformation", FormMethod.Post, new AjaxOptions { HttpMethod = "Post", InsertionMode = InsertionMode.Replace, UpdateTargetId = "divFormContainerMain", LoadingElementId = "divPreLoader", OnSuccess = "onSuccess" }))
{
<div id="divPreLoader" style="display:none; text-align: center"><img src="@Url.Content("~/Content/images/preLoader.gif")" alt="" /></div>
<div id="divFormContainerMain">
@Html.Partial("_EditPartialView", Model)
</div>
<div class="buttonContainerBottom">
<span class="buttonContainerInner">
<input type="submit" id="btnSave" name="buttonPress" value="save" class="orangeButton" />
</span>
</div>
}
_EditPartialView.cshtml:
@Html.ValidationSummary(false)
<div id="divFormContainerUserInformation" class="formContainer">
<div class="labelContainer">
@Html.LabelFor(m => m.UserName)
</div>
<div class="elementContainer">
@Html.TextBoxFor(m => m.UserName, new { style = "width: 200px" })
@Html.ValidationMessageFor(m => m.UserName)
</div>
<div class="labelContainer">
@Html.LabelFor(m => m.Name)
</div>
<div class="elementContainer">
@Html.TextBoxFor(m => m.Name, new { style = "width: 200px" })
@Html.ValidationMessageFor(m => m.Name)
</div>
<div class="labelContainer">
@Html.LabelFor(m => m.EmailAddress)
</div>
<div class="elementContainer">
@Html.TextBoxFor(m => m.EmailAddress, new { style = "width: 200px" })
@Html.ValidationMessageFor(m => m.EmailAddress)
</div>
.
.
.
.
.
.
</div>
UserController:
[HttpPost]
public ActionResult Edit(UserModel userModel)
{
ModelState.AddModelError("", "This is a test");
return PartialView("_EditPartialView", userModel);
}