Ajax.BeginForm not work in ASP.NET MVC 4 and 4.5

2014-01-24


In newer versions of ASP.NET MVC such as 4 or 4.5, when you used Ajax.BeginForm like

@using (Ajax.BeginForm("Index", "MyController”, 
             new AjaxOptions() 
             { 
                 UpdateTargetId = "chart", 
                 InsertionMode = InsertionMode.Replace, 
                 HttpMethod = "post", 
                 OnSuccess = "loadChart",
             } 
         )) 
{ 
    <div> 
        <input type="submit" value="Load Chart" /> 
    </div> 
}

You might find it will not work but the web page refresh like a regular post action, the reason is that you have to add a js file "jquery.unobtrusive-ajax.js" .

You can use NuGet package to update or install:

image

Or right click your project and use Manage NuGet Packages to install:

image

After installed, you will get 2 more .js files under your Scripts folder, then you just drag the min file into your View page:

<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>