O'Reilly logo

Professional ASP.NET MVC 4 by Scott Hanselman, K. Scott Allen, Brad Wilson, Phil Haack, Jon Galloway

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Ajax Helpers

You've seen the HTML helpers in ASP.NET MVC. You can use the HTML helpers to create forms and links that point to controller actions. There is also a set of Ajax helpers in ASP.NET MVC. Ajax helpers also create forms and links that point to controller actions, but they behave asynchronously. When using these helpers, you don't need to write any script code to make the asynchrony work.

Behind the scenes, these Ajax helpers depend on the unobtrusive MVC extensions for jQuery. To use the helpers, you need to have the jquery.unobtrusive-ajax script present. Note that a default MVC 4 project will include this script in the application's _Layout view by default. If you want to include the file manually, you could use the following script tags:

<script src="∼Scripts/jquery-1.6.2.min.js")
></script>  
<script  src="∼/Scripts/Scripts/jquery.unobtrusive-ajax.min.js")
         ></script>

@RenderSection("scripts", required:false);

Ajax ActionLinks

Ajax helpers are available through the Ajax property inside a Razor view. Like HTML helpers, most of the methods on this property are extension methods (except for the AjaxHelper type).

The ActionLink method of the Ajax property creates an anchor tag with asynchronous behavior. Imagine you want to add a “daily deal” link at the bottom of the opening page for the MVC Music Store. When users click the link, you don't want them to navigate to a new page, but you want the existing page to magically display the details of a heavily discounted album. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required