4

我正在尝试使用 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);
}
4

2 回答 2

6

您的脚本在哪里添加?在 _layout.cshtml 中还是在视图本身中?你是如何加载视图的?是否有显示部分视图的 ajax 请求?

如果您通过 ajax 或作为局部视图加载局部视图,则可能是局部视图尚未加载到 jquery DOM 模型树中。

我会尝试以下。改变

<div id="divFormContainerMain">
    @Html.Partial("_EditPartialView", Model)
</div>

<div id="divFormContainerMain">
    @Html.Partial("_EditPartialView", Model)
    <script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
    <script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
</div>

或者

<div id="divFormContainerMain">
    @Html.Partial("_EditPartialView", Model)
    @Scripts.Render("~/bundles/jqueryval") //if you have a bundle for it
</div>

无论如何,我的建议是仅在需要时才加载验证和不显眼的脚本,而不是在 _layout.cshtml 页面中。

另外不要忘记在 web.config 中启用以下 appSettings

    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
于 2013-07-25T09:18:24.763 回答
5

确保jquery.unobtrusive-ajax.js在 jquery 本身之后将脚本包含到视图中。否则 Ajax.BeginForm 助手不会做你认为它做的事情:

<script type="text/javascript" src="@Url.Content("~/scripts/jquery-YOUR-VERSION.js")"></script>
<script type="text/javascript" src="@Url.Content("~/scripts/jquery.unobtrusive-ajax.js")"></script>
于 2013-07-24T20:33:00.503 回答