Internet project MVC4
I use Ajax.BeginForm to do a postback with validation, and it returns the whole page, not just UpdateTargetID. I looked at other posts on SO and did not find an answer. I built a new MVC4 Internet project for testing purposes only (VS 2012 was updated using "ASP.NET and Web Tools 2012.2").
Here is my code
controller
public ActionResult Index() { var vM = _db.Students.FirstOrDefault(); return View(vM); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Index(Student vM) { if (ModelState.IsValid) { //code if Model valid return Json(new { url = Url.Action("About", "Controller") }); } ModelState.AddModelError(string.Empty, "AJAX Post"); return PartialView("Index", vM); }
View
@model AJAX_Test.Models.Student @{ ViewBag.Title = "Student"; } <script src="~/Scripts/jquery-1.8.2.js"></script> <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script> <script type="text/javascript"> var onSuccess = function (result) { if (result.url) { window.location.href = result.url; } } </script> <h1>@ViewBag.Title</h1> <div id="IDXForm"> @using (Ajax.BeginForm("Index", new AjaxOptions() { UpdateTargetId = "IDXForm", OnSuccess = "onSuccess", HttpMethod = "Post" })) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) <span>@Html.EditorFor(m => m.FirstName) @Model.EnrollmentDate.ToShortDateString()</span> <input type="submit" value="Submit" /> } </div>
Initial view: 
After sending: 
The source code for the body after sending:
<div id="body"> <section class="content-wrapper main-content clear-fix"> <script src="/Scripts/jquery-1.8.2.js"></script> <script src="/Scripts/jquery.unobtrusive-ajax.js"></script> <script type="text/javascript"> var onSuccess = function (result) { if (result.url) { window.location.href = result.url; } } </script> <h1>Student</h1> <div id="IDXForm"> <form action="/" data-ajax="true" data-ajax-method="Post" data-ajax-mode="replace" data-ajax-success="onSuccess" data-ajax-update="#IDXForm" id="form0" method="post"><input name="__RequestVerificationToken" type="hidden" value="vkCszJu-fKT6zUr5ys2StOTPF6a9pZdj5k1MyaAZKo8MPweS53dUuni0C9B17NjL_GVydHa7-jI1H0F9HrYEdKxeCWq9mCeER3ebaZYLxIs1" /><span><input class="text-box single-line" id="FirstName" name="FirstName" type="text" value="Carson" /> 9/1/2005</span> <input type="submit" value="Submit" /> </form></div> " post "> <input name =" __ RequestVerificationToken "type =" hidden "value =" vkCszJu-fKT6zUr5ys2StOTPF6a9pZdj5k1MyaAZKo8MPweS53dUuni0C9B17NjL_GVydHa7-jI1H0F9HrYEdKxeCWq9mCeER3ebaZYLxIs1 "/> <span> <input class =" text-box single-line " <div id="body"> <section class="content-wrapper main-content clear-fix"> <script src="/Scripts/jquery-1.8.2.js"></script> <script src="/Scripts/jquery.unobtrusive-ajax.js"></script> <script type="text/javascript"> var onSuccess = function (result) { if (result.url) { window.location.href = result.url; } } </script> <h1>Student</h1> <div id="IDXForm"> <form action="/" data-ajax="true" data-ajax-method="Post" data-ajax-mode="replace" data-ajax-success="onSuccess" data-ajax-update="#IDXForm" id="form0" method="post"><input name="__RequestVerificationToken" type="hidden" value="vkCszJu-fKT6zUr5ys2StOTPF6a9pZdj5k1MyaAZKo8MPweS53dUuni0C9B17NjL_GVydHa7-jI1H0F9HrYEdKxeCWq9mCeER3ebaZYLxIs1" /><span><input class="text-box single-line" id="FirstName" name="FirstName" type="text" value="Carson" /> 9/1/2005</span> <input type="submit" value="Submit" /> </form></div>
Can anyone understand what is wrong with my code?
Thanks.
Joe
source share