28.04.2014 Views

ASP.NET MVC 4 in Action - Manning Publications

ASP.NET MVC 4 in Action - Manning Publications

ASP.NET MVC 4 in Action - Manning Publications

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

116 CHAPTER 7 Ajax <strong>in</strong> <strong>ASP</strong>.<strong>NET</strong> <strong>MVC</strong><br />

<strong>ASP</strong>.<strong>NET</strong> <strong>MVC</strong> Ajax helpers<br />

jQuery unobtrusive Ajax adapter<br />

Microso Ajax adapter<br />

jQuery<br />

Microso Ajax<br />

Nave browser JavaScript<br />

Figure 7.5 The relationship<br />

between <strong>ASP</strong>.<strong>NET</strong> <strong>MVC</strong> Ajax<br />

helpers and JavaScript libraries<br />

7.2.1 Ajax.<strong>Action</strong>L<strong>in</strong>k<br />

We’ll beg<strong>in</strong> by tak<strong>in</strong>g the same example of the Privacy Policy page from section 7.1.2<br />

and look at how we can use the <strong>Action</strong>L<strong>in</strong>k Ajax helper to achieve the same result.<br />

Our controller does not need to be modified, but our Index view will need the<br />

changes shown <strong>in</strong> the follow<strong>in</strong>g list<strong>in</strong>g.<br />

List<strong>in</strong>g 7.8 Us<strong>in</strong>g Ajax.<strong>Action</strong>L<strong>in</strong>k<br />

@section head { <br />

<br />

<br />

}<br />

B<br />

Render <strong>in</strong> head section<br />

C<br />

Reference<br />

jQuery.unobtrusive<br />

script<br />

@Ajax.<strong>Action</strong>L<strong>in</strong>k(<br />

Def<strong>in</strong>e hyperl<strong>in</strong>k<br />

"Show the privacy Policy", text E <strong>Action</strong> to<br />

"PrivacyPolicy", <br />

l<strong>in</strong>k to<br />

new AjaxOptions { <br />

F Additional<br />

InsertionMode = InsertionMode.Replace, <br />

options<br />

UpdateTargetId = "privacy" <br />

})<br />

<br />

As with our previous examples, we beg<strong>in</strong> by render<strong>in</strong>g part of the head section B. But<br />

this time we also add a reference to the jQuery.unobtrusive-ajax.js file C, which also<br />

comes as part of the default <strong>ASP</strong>.<strong>NET</strong> <strong>MVC</strong> project template. This is the adapter that<br />

knows how to use jQuery to perform Ajax calls based on the elements that we render.<br />

Next we <strong>in</strong>clude a call to Ajax.<strong>Action</strong>L<strong>in</strong>k. There are several overloads for this<br />

method, but the one we’re us<strong>in</strong>g has three arguments. The first is the text that<br />

should become a hyperl<strong>in</strong>k D. The second is the name of the action that should be<br />

<strong>in</strong>voked asynchronously E—<strong>in</strong> this case, our PrivacyPolicy action. The f<strong>in</strong>al argument<br />

is an AjaxOptions object that can be used to customize the Ajax request F.<br />

D

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!