in

Platinum Bay

Peace, Love, and Visual Studio ALM

This Blog

Syndication


.NETicated

jQuery and MVC: JSON

I strongly dislike synchronous browser requests: the request has to be sent to the server, server action is performed, a server response is received, and the page is re-rendered which usually involves reloading images, style sheets, JavaScript files, etc. It takes time, sends an awful lot of traffic across the wire, and it doesn’t provide for the best user experience. In some scenarios this makes sense, but in most cases I find it to be an annoyance.

Enter jQuery to the rescue. jQuery is the most exciting innovation to happen to JavaScript since, well, maybe JavaScript itself. One of the jQuery features I enjoy the most is the AJAX functionality which allows jQuery to talk to the server asynchronously, or behind the scenes.

I personally like to use the JSON (JavaScript Object Notion) format, which is lighter-weight than XML. Using jQuery, a JSON GET request is written as:

$.getJSON('[url]', { [parameters] }, function(json) {
// callback function code
});

Let’s say I want to perform an asynchronous search in my web page for people names without having to reload the page. In MVC, I need an action to handle the request:

public class SearchController : Controller
{
string[] peopleNames = new[] { "Bob", "Steve", "Joe", "Frank", "Lou" };

public JsonResult Search(string q)
{
var results = peopleNames.Where(n => n.Contains(q));

if (results.Count() > 0)
return new JsonResult { Data = new { Success = true, Results = results } };
else
return new
JsonResult { Data = new { Success = false } };
}
}

The first thing to notice is the built-in JsonResult type on the Search action. MVC understands and is able to handle JSON data. Simply return a JsonResult object and populate the Data property with the data to be returned. The serialization of that data is handled under the covers.

Back in the MVC View, call getJSON to make a request to the action and handle the response:

$.getJSON('<%= Url.Action("Search", "Search") %>', { q: $('input#search') }, function(json) {
if (json.Success) {
// results found
}
else {
// nothing found
}
});
Note that Url.Action is used to get the virtual url. Url.Action and Url.Content are great ways to handle relative paths. Next, the search query is passed from an input field with an ID of search. And finally, the callback is handled. jQuery handles the deserialization under the covers, and the values can be handled as simple properties.

The introduction of ASP.NET MVC and jQuery make asynchronous calls very quick and easy, and help to provide the end user with a better experience. In future posts, we’ll look at more specific examples of utilizing jQuery and MVC to provide a better user experience.

Published Aug 28 2009, 07:09 PM by Steve
Filed under: , ,

Comments

August 30, 2009 11:14 PM

Pingback from  ASP.NET MVC Archived Blog Posts, Page 1

 

August 31, 2009 6:56 AM

Pingback from  Dew Drop &#8211; August 31, 2009 | Alvin Ashcraft&#039;s Morning Dew

 

August 31, 2009 2:29 PM

Hey Steve,

How is a timeout handled in this case?

Mike
 

August 31, 2009 7:35 PM

In my last two posts ( jQuery and MVC: JSON and Favorite Image Icons with jQuery and MVC ) I covered

 

May 21, 2010 11:18 PM

Pingback from  Model Dav S550, Ford E 550 Super Duty Driving Light

 

May 22, 2010 12:43 AM

Pingback from  Sale 2006 Suzuki Aerio Premium, Motor Aerio 2006 Suzuki Sx4

 

May 22, 2010 3:26 PM

Pingback from  C30 Headlight Automotive, C30 Pickup Pt

 

May 22, 2010 10:18 PM

Pingback from  Relay 1 Price Red 2003 Saturn Vue, Saturn Vue Car Stereo Removal

 

May 23, 2010 8:42 AM

Pingback from  10 Discount Subaru Gl Radiator Cap Assembly, Subaru Svx Replacement Lsi - 211.tvshowzone.com

 

May 24, 2010 11:12 AM

Pingback from  260z Dachshund Car, 260z Replacement Datsun 240z Door Nissan 300zx - 437.tijuanareader.com

 

May 24, 2010 11:30 AM

Pingback from  Brother Pt 2100, Laguna Pressure Flo 2100 - 282.zapstreaming.com

 

May 24, 2010 11:52 AM

Pingback from  2003 Suzuki Gsxr 600 Parts, 1989 Suzuki Swift Radiator Mileage Timing Belt - 91.ja3ra.com

 

May 24, 2010 1:27 PM

Pingback from  2002 Chevrolet K3500 For Sale Trailblazer Ext, 2005 Trailblazer Parts Upgrade Mirror - 75.zapstreaming.com

 

May 24, 2010 2:52 PM

Pingback from  Parts Used Toyota Camry, 2000 Camry Projector Headlights Nissan Altima - 158.codebluehacks.org

 

May 24, 2010 3:22 PM

Pingback from  Ramcharger Replacement Fit Bumper Filler, Used D150 Classified Dodge Ramcharger - 492.luna-atra.net

 

May 24, 2010 5:59 PM

Pingback from  Mercedes Benz 450sel Parts Cl65 Amg, Mercedes Benz 450se Part Brake Master Cylinder S65 Amg - 6.akemet.com

 

May 24, 2010 6:48 PM

Pingback from  Volvo S40 V40 Headlight Lens 01, V40 Globe - 440.defutbolazo.com

 

May 24, 2010 7:19 PM

Pingback from  Karmann Ghia Promotion, Karmann Ghia Supercharger Judson - 82.myipgirl.com

 

May 24, 2010 7:32 PM

Pingback from  Repair 1997 Mazda B2300, Mazda Rx7 Specifications - 30.akemet.com

 

May 24, 2010 8:21 PM

Pingback from  1999 Mercury Tracer User Guide, Philips Digital Voice Tracer 660 Price - 94.luna-atra.net

 

May 24, 2010 8:33 PM

Pingback from  Capri Shoe Repair, Caprice Theatre Whiterock - 280.jeepsunlimted.com

 

May 25, 2010 4:48 AM

Pingback from  Pontiac 6000 Yahoo, Pontiac 6000 Auto Parts Replacement Mirror - 270.unlockiphone30.net

 

May 25, 2010 5:21 AM

Pingback from  Sprint Cell Phone Customer Service, 1500 Sprinter Search Used Cars - 430.jeepsunlimted.com

 

May 25, 2010 8:12 AM

Pingback from  Mighty Max Jc Whitney, Pc Mighty Max Safe - 118.eumreborn.com

 

May 25, 2010 5:13 PM

Pingback from  2002 Geo Tracker Rent, Yacker Tracker Siren Mp3 - 383.renters.ws

 

May 26, 2010 12:42 AM

Pingback from  Sd550 Compare Prices Bathroom Radiators, Mercedes Benz S550 Headlight Lexus Ls - 277.renters.ws

 

May 26, 2010 12:55 AM

Pingback from  Discount Volvo V50 T5 Specs, Teknisk Spec Volvo V50 - 7.luna-atra.net

 

May 27, 2010 10:51 AM

Pingback from  2005 - 2002 @ Ssr Italian Forum, Ssr 70cc Pit Bike 150cc - 217.unlockiphone30.net

 

May 27, 2010 2:05 PM

Pingback from  2000 - 1991 @ Subaru Forester Aftermarket Headlights Turbo, Subaru Forester Hitch Forum - 44.tvshowzone.com

 

May 27, 2010 2:32 PM

Pingback from  2009 - 1980 @ Sigma Parts New Mitsubishi Eastern Catalytic Converter 409 Stainless Steel, Mitsubishi Endeavor Coupon Rims - 117.computeronlinebingo.com

 

May 27, 2010 2:34 PM

Pingback from  2000 - 1999 @ Discount 2005 Bmw 545i Luxury Sedans Comfort, 545i Custom Offer - 499.jordanbrandallamerican.com

 

May 30, 2010 11:29 PM

Pingback from  1991 - 2003 @ 2000 Radiator 2004 Chevrolet Avalanche Coolant Recovery Tank, Chevrolet Avalanche Radiator Cold Air Intake Vent Visor - 217.mfbattle.com

 

May 31, 2010 2:51 AM

Pingback from  2005 - 1992 @ 1950 Willys For Sale, Fotos De Jeep Willys Cj3a - 130.jeepsunlimted.com

 

May 31, 2010 2:53 AM

Pingback from  1988 - 2009 @ Monza Auction Phil Hill, Part Coldfusion Mx Photoshop Lightroom - 284.luna-atra.net

 

May 31, 2010 6:11 AM

Pingback from  2001 - 2006 @ 280c Halogen Bulb Sylvania, Buy Mercedes Benz 280ce Shock Absorber - 57.cmanager.org

 

May 31, 2010 6:16 AM

Pingback from  1989 - 2008 @ Yukon Xl Sale Ohio Gmc Sonoma, Gmc Sonoma Performance Parts Replacement - 417.tgrconversions.com

 

May 31, 2010 6:17 AM

Pingback from  1981 - 2007 @ Buy Mercedes Benz S350 S65 Amg, Part Salvage Lexus Es350 - 59.dlmreza.net

 

May 31, 2010 7:25 AM

Pingback from  2001 - 2006 @ Buy Gmc K15 K2500 Pickup Axle Assembly, Minolta Bizhub C250 Device - 74.1fh.org

 

May 31, 2010 6:52 PM

Pingback from  2001 - 2006 @ 2003 Mercury Mountaineer Convenience, Mountaineer Aftermarket Next - 402.binggreen.com

 

May 31, 2010 7:42 PM

Pingback from  2002 - 1980 @ Inspiron 6000 Replacement Battery, Dv6000 Charger - 54.luna-atra.net

 

May 31, 2010 8:20 PM

Pingback from  1992 - 2001 @ Mkx Free Shipping, Lincoln Mkx Discount Awd Ford Edge - 172.tgrconversions.com

 

May 31, 2010 8:46 PM

Pingback from  1998 - 1980 @ Vanagon Mass Air Flow Sensor Parts, Shimano Used Volkswagen Vanagon - 70.jeepsunlimted.com

 

May 31, 2010 8:58 PM

Pingback from  1988 - 1981 @ Lhs Headlight Silverstar Concorde Chrysler Sebring, 1998 Chrysler Concorde Transmission Control Module - 29.tgrconversions.com

 

May 31, 2010 9:06 PM

Pingback from  1990 - 1986 @ 325ix Radiator Product Description Bmw Brand New, 325ix Used Engine 1989 Bmw Series - 329.myipgirl.com

 

May 31, 2010 9:18 PM

Pingback from  1988 - 2009 @ 2000 Volkswagen Passat Pricing, Volkswagen Passat Tube Also Use - 20.1fh.org

 

Leave a Comment

(required )  
(optional )
(required )  
Add

About Steve

Steve Andrews is an independent consultant, INETA speaker, and Microsoft MVP for Visual Studio ALM. He has been working in technology for over ten years focusing on custom application development and Application Lifecycle Management. Steve is also Microsoft and IBM certified and a community fanatic having led sessions at nearly 100 events across North America. When he's not developing software solutions or engaging with the community about software technology, Steve is a closet singer and songwriter and plays the guitar and keys. Occasionally, Steve even gets to sleep. Occasionally.
Powered by Community Server (Commercial Edition), by Telligent Systems
© Platinum Bay | Some Rights Reserved Creative Commons License

Disclaimer: The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Feel free to challenge me, disagree with me, or tell me I'm completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or annonymous comments) - so keep it polite, please.