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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Ajax with JSON and client templates<br />

123<br />

(cont<strong>in</strong>ued)<br />

If an authenticated user were to visit this malicious site, then the data would be<br />

downloaded and the malicious site could get access to it. We’ll explore JSON hijack<strong>in</strong>g<br />

<strong>in</strong> the next chapter.<br />

In our particular example, we aren’t return<strong>in</strong>g sensitive data, so it is perfectly safe to<br />

enable JSON responses to GET requests.<br />

Next, we’ll implement the Index view.<br />

List<strong>in</strong>g 7.14 The speaker list page<br />

@model IEnumerable<br />

<br />

<br />

Speakers<br />

CSS reference C<br />

D<br />

Strongly<br />

typed<br />

B view<br />

Custom script<br />

reference<br />

<br />

@foreach (var speaker <strong>in</strong> Model) { <br />

<br />

@Html.<strong>Action</strong>L<strong>in</strong>k(speaker.FullName, "Details",<br />

new { id = speaker.Id }) <br />

<br />

} <br />

<br />

Generate list E of speakers<br />

<br />

<br />

conta<strong>in</strong>er<br />

We beg<strong>in</strong> by ensur<strong>in</strong>g that our view is strongly typed to an IEnumerable B,<br />

which corresponds to the list of speakers be<strong>in</strong>g passed to the view from the controller.<br />

Next, we <strong>in</strong>clude a reference to a CSS stylesheet C, followed by a reference to a script<br />

file that will conta<strong>in</strong> our client-side code D.<br />

We then loop over all of the speakers, creat<strong>in</strong>g an unordered list conta<strong>in</strong><strong>in</strong>g their<br />

names with<strong>in</strong> a hyperl<strong>in</strong>k E.<br />

Follow<strong>in</strong>g this, we add an image to the page that will be displayed while the Ajax<br />

request is <strong>in</strong> progress F (also known as a sp<strong>in</strong>ner).<br />

F<strong>in</strong>ally, we have a element that will be used to display the speaker’s details<br />

after they’ve been fetched from the server G. We won’t be us<strong>in</strong>g this just yet, but we’ll<br />

make use of it <strong>in</strong> section 7.3.2.<br />

Now that we have our view implemented, we can implement our client-side code<br />

with<strong>in</strong> the Speakers.js file.

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

Saved successfully!

Ooh no, something went wrong!