29.05.2015 Views

o_19mgorv9t13a3ko71fev19l81mqa.pdf

Create successful ePaper yourself

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

separating the way that a request has been made from the data format that the client seeks. This is my preferred approach and the<br />

reason I tend to define separate action methods for data formats.<br />

I also need to make two changes to the GetPeople.cshtml view to support the single action method, as shown in<br />

Listing 23-21.<br />

Listing 23-21. Supporting a Single Data Action Method in the GetPeople.cshtml File<br />

@using HelperMethods.Models<br />

@model string<br />

@{<br />

ViewBag.Title = "GetPeople";<br />

Layout = "/Views/Shared/_Layout.cshtml";<br />

AjaxOptions ajaxOpts = new AjaxOptions {<br />

Url = Url.Action("GetPeopleData"),<br />

LoadingElementId = "loading",<br />

LoadingElementDuration = 1000,<br />

OnSuccess = "processData"<br />

};<br />

}<br />

<br />

function processData(data) {<br />

var target = $("#tableBody");<br />

target.empty();<br />

for (var i = 0; i < data.length; i++) {<br />

var person = data[i];<br />

target.append("" + person.FirstName + ""<br />

+ person.LastName + "" + person.Role<br />

+ "");<br />

}<br />

}<br />

<br />

Get People<br />

<br />

Loading Data...<br />

<br />

<br />

FirstLastRole<br />

<br />

@Html.Action("GetPeopleData", new {selectedRole = Model })<br />

<br />

<br />

@using (Ajax.BeginForm(ajaxOpts)) {<br />

<br />

@Html.DropDownList("selectedRole", new SelectList(<br />

new [] {"All"}.Concat(Enum.GetNames(typeof(Role)))))<br />

Submit<br />

<br />

}<br />

<br />

631

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

Saved successfully!

Ooh no, something went wrong!