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.

Figure 23-9. Working with JSON data instead of HTML fragments<br />

Preparing Data for Encoding<br />

When I called the Json method from within the GetPeopleDataJson action method, I left the MVC Framework to<br />

figure out how to encode People objects in the JSON format. The MVC Framework doesn’t have any special insights into the<br />

model types in an application, and so it makes a best-effort guess about what it needs to do. Here is how the MVC Framework<br />

expresses a single Person object in JSON:<br />

...<br />

{"PersonId":0,"FirstName":"Adam","LastName":"Freeman",<br />

"BirthDate":"\/Date(62135596800000)\/","HomeAddress":null,"IsApproved":false,"Role":0}<br />

...<br />

It looks like a bit of a mess, but the result is actually pretty clever—it just isn’t quite what I need. First, all the properties defined<br />

by the Person class are represented in the JSON, even though I did not assign values to some of them in the People<br />

controller. In some cases, the default value for the type has been used (false is used for IsApproved, for example), and<br />

in others null has been used (such as for HomeAddress). Some values are converted into a form that can be readily<br />

interpreted by JavaScript, such as the BirthDate property, but others are not handled as well, such as using 0 for the<br />

Role property rather than Admin.<br />

VIEWING JSON DATA<br />

It can be useful to see what JSON data your action methods return and the easiest way to do this is to enter a URL that<br />

targets the action in the browser, like this:<br />

http://localhost:13949/People/GetPeopleDataJson?selectedRole=Guest<br />

You can do this in pretty much any browser, but most will force you to save and open a text file before you can see the<br />

JSON content. I like to use the Google Chrome browser for this because it helpfully displays the JSON data in the main<br />

browser window, which makes the process quicker and means you don’t end up with dozens of open text file windows. I<br />

also recommend Fiddler (www.fiddler2.com), which is an excellent Web debugging proxy that allows you to dig<br />

right into the details of the data sent between the browser and the server.<br />

The MVC Framework has made a good attempt, but I end up sending properties to the browser that I don’t subsequently use<br />

and the Role value isn’t expressed in a useful way. This is a typical situation when relying on the default JSON encoding, and<br />

some preparation of the data you want to send the client is usually required. In Listing 23-19, you can see how I have revised the<br />

GetPeopleDataJson action method in the People controller to prepare the data I pass to the Json method.<br />

628

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

Saved successfully!

Ooh no, something went wrong!