HomeLearnASP.NET MVC VideosHow Do I: Return JSON Formatted Data for an AJAX Call in an ASP.NET MVC Web Application?  

How Do I: Return JSON Formatted Data for an AJAX Call in an ASP.NET MVC Web Application?

Please install Silverlight or click download to watch video locally.

In this video Chris Pels shows how to use the JsonResult and Json classes to return instances of classes as JSON formatted data. First, a sample MVC web application is created and a controller and associated view are added for customer information. Then a simple customer class is created as part of the data model. A GetCustomer() function is then added to the customer controller to retrieve data for a single customer in JSON format. Finally, script is added to the customer view that uses jQuery to make an AJAX call to GetCustomer() to retrieve the customer data in JSON format

Presented by Chris Pels

Duration: 15 minutes, 48 seconds

Date: 22 October 2009

Watch    Video   |   Download    Video   |   VB Code    C# Code

Video downloads: WMV  |  Zune  |  iPod  |  PSP  |  MPEG-4  |  3GP

Audio downloads: AAC  |  WMA  |  MPEG-4  |  MPEG-3  |  MPEG-2

Comments : 5

Leave a Comment

dewey2009 : On October 23, 2009 3:46 PM said:

Here are two things to be aware of:

1) You'll need to define a class in the Models folder.

public class Customer

{

public string CustomerName { get; set; }

public string City { get; set; }

}

2) This video works with VS 2008/MVC 1. If you are using MVC 2 or Visual Studio 2010 Beta 2, then you'll need to make a slight adjustment.

// MVC 1 code

public JsonResult GetCustomer()

{

Models.Customer customer = new Models.Customer();

customer.CustomerName = "Microsoft";

customer.City = "Redmond";

return Json(customer);

}

// MVC 2 or VS 2010 Beta 2 code

public JsonResult GetCustomer()

{

Models.Customer customer = new Models.Customer();

customer.CustomerName = "Microsoft";

customer.City = "Redmond";

return Json(mc, JsonRequestBehavior.AllowGet);

}

gardavis : On October 23, 2009 4:44 PM said:

> return Json(mc, JsonRequestBehavior.AllowGet);

Do you mean this?:

return Json(customer, JsonRequestBehavior.AllowGet);

dewey2009 : On October 23, 2009 5:19 PM said:

Gardavis,

Thanks for the correction. I forgot to change mc to customer.

rafek : On October 24, 2009 5:25 AM said:

Short but helpfull video, thanks.

gerrylowry : On November 12, 2009 2:29 AM said:

"Chris Pels shows how to use the JsonResult and Json classes to return instances of classes as JSON formatted data."

(1) a transcript of each video would be useful.

(2) assuming that these videos are for BEGINNERs too,

it would be a very good idea to briefly say

(a) what JSON is, and

(b) when and why one might use JSON.

http://json.org/

"JSON (JavaScript Object Notation) is a lightweight data-interchange format.

... easy for humans to read and write.

... easy for machines to parse and generate.

... based on a subset of the JavaScript ...

JSON is a text format that is completely language independent ...

... JSON an ideal data-interchange language."

It is important in learning materials to explain "why" and not just "how"; example, in the sixth minute, Chris talks about a JSON function and an object in JSON format without explaining either. In the seventh minute, Chris say "Let's use jQuery ..." without saying why jQuery* instead of for example, just JavaScript.

*jQuery [http://jquery.com/] "... a fast and concise JavaScript Library ..."

There is an ancient and well worn teaching paradigm that is seldom used in Microsoft videos:

(a) tell the student the lesson plan and goals

(b) deliver (a)

(c) recapitulate (b)

Regards,

Gerry (Lowry)

Leave a Comment

You must be logged in to leave a comment. Click here to log in.

Microsoft Communities