Ajax.BeginForm无法使用Html.ValidationSummary

时间:2013-07-24 20:25:27

标签: asp.net-mvc asp.net-mvc-3 asp.net-ajax

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

2 个答案:

答案 0 :(得分: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" />

答案 1 :(得分:5)

确保在jquery之后将jquery.unobtrusive-ajax.js脚本包含在视图中。否则,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>