Ajax.BeginForm not working with Html.ValidationSummary

asp.net-ajax, asp.net-mvc, asp.net-mvc-3

Solution

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>

Problem

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

Original source