Ajax.BeginForm不Html.ValidationSummary工作 [英] Ajax.BeginForm not working with Html.ValidationSummary

查看:153
本文介绍了Ajax.BeginForm不Html.ValidationSummary工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Ajax.BeginForm将数据发布到控制器。在特定的错误的情况下,该形式应当重新呈现,并显示加入到ModelState中定制的错误消息。出于某种原因,该错误信息不会显示。我想,即使是不工作下面的测试情况下,我失去的东西吗?

  Edit.cshtml:
@using(Ajax.BeginForm(编辑,UserInformation,FormMethod.Post,新AjaxOptions {列举HTTPMethod =邮报,InsertionMode = InsertionMode.Replace,UpdateTargetId =divFormContainerMain,LoadingElementId =格preLoader的onSuccess =的onSuccess}))
{
    < D​​IV ID =格preLoader的风格=显示:无;文本对齐:中心>< IMG SRC =@ Url.Content(〜/内容/图片/ preLoader。 GIF),ALT =/>< / DIV>
    < D​​IV ID =divFormContainerMain>
        @ Html.Partial(_ EditPartialView模型)
    < / DIV>
    < D​​IV CLASS =buttonContainerBottom>
        <跨度类=buttonContainerInner>
            <输入类型=提交ID =btnSaveNAME =按钮preSSVALUE =保存级=orangeButton/>
        < / SPAN>
    < / DIV>
}
_EditPartialView.cshtml:
@ Html.ValidationSummary(假)
< D​​IV ID =divFormContainerUserInformation级=formContainer>
    < D​​IV CLASS =labelContainer>
        @ Html.LabelFor(M = GT; m.UserName)
    < / DIV>
    < D​​IV CLASS =elementContainer>
        @ Html.TextBoxFor(M = GT; m.UserName,新{风格=宽度:200像素})
        @ Html.ValidationMessageFor(M = GT; m.UserName)
    < / DIV>
    < D​​IV CLASS =labelContainer>
        @ Html.LabelFor(M = GT; m.Name)
    < / DIV>
    < D​​IV CLASS =elementContainer>
        @ Html.TextBoxFor(M = GT; m.Name,新{风格=宽度:200像素})
        @ Html.ValidationMessageFor(M = GT; m.Name)
    < / DIV>
    < D​​IV CLASS =labelContainer>
        @ Html.LabelFor(M = GT; m.EmailAddress)
    < / DIV>
    < D​​IV CLASS =elementContainer>
        @ Html.TextBoxFor(M = GT; m.EmailAddress,新{风格=宽度:200像素})
        @ Html.ValidationMessageFor(M = GT; m.EmailAddress)
    < / DIV>
    。
    。
    。
    。
    。
    。
< / DIV>UserController的:
[HttpPost]
公众的ActionResult编辑(的usermodel的usermodel)
{
    ModelState.AddModelError(,这是一个测试);
    返回PartialView(_ EditPartialView的usermodel);
}


解决方案

请确认您已经包含了 jquery.unobtrusive-ajax.js 脚本的jQuery后您的看法本身。否则,Ajax.BeginForm助手不会做你认为它的作用:

 <脚本类型=文/ JavaScript的SRC =@ Url.Content(〜/脚本/ jQuery的-YOUR-VERSION.js)>< /脚本>
<脚本类型=文/ JavaScript的SRC =@ Url.Content(〜/脚本/ jquery.unobtrusive-ajax.js)>< / SCRIPT>

I am trying to use the Ajax.BeginForm to post data to a controller. In the case of specific errors, the form should re-render and display the custom error message that was added to the ModelState. For some reason, the error message is not displaying. I am even trying the following test case which is not working, am I missing something?

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);
}

解决方案

Make sure that you have included the jquery.unobtrusive-ajax.js script to your view after jquery itself. Otherwise the Ajax.BeginForm helper won't do what you think it does:

<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>

这篇关于Ajax.BeginForm不Html.ValidationSummary工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆