I need to refresh a div with a PartialView by using Ajax.BeginForm. I have done this over a dozen times in MVC4 and it worked flawlessly. In MVC5, it doesn't work though :(
Here are the steps I took:
Code for this _Test.cshtml view:
<p>From now on I'm gonna change my life: @DateTime.Now.ToString()</p>
Edit the Views/Home/Index.cshtml view:
@{
ViewBag.Title = "Home Page";
}
@using (Ajax.BeginForm("ChangeLife", "Home", null, new AjaxOptions() { UpdateTargetId = "test", HttpMethod = "Post" }, null))
{
<input type="submit" value="Start" />
}
<div id="test">
@Html.Partial("_Test")
</div>
Put this in your HomeController:
public ActionResult ChangeLife()
{
return this.PartialView("_Test");
}
If I click Manage NuGet packages, jQuery and Microsoft jQuery Unobtrusive Validation are installed by default.
I had to install Microsoft jQuery Unobtrusive Ajax and render the javascript bundles for jqueryval (existing but not rendered) and added a new one for the unobtrusive.ajax script files.
new bundle in BundleConfig class:
bundles.Add(new ScriptBundle("~/bundles/jqueryajax").Include(
"~/Scripts/jquery.unobtrusive*"));
added render methods for the two bundles in _Layout.cshtml (underneath the jquery render):
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/jqueryajax")