Home / ASP.NET Wiki / Javascript / AJAX / JSON / Json With Jquery MVC3

Json With Jquery MVC3

 Rate It (2)

Hi friends,

I see that most of us know Json in forums, but there are a lot of people that starts working with that and I create a simple example that show How Json work, and not only that, because with this example you can see in action the Model Binder that MVC3 have by default and in MVC2 we need to do some little more things.

 For this example you need to add the library Json2 created by Douglas Crockford.

View:(Jquery)

<script src="../../Scripts/json2.js" type="text/javascript"></script> 

 

<input type="button" onclick="javascript:JsonInAction()" value="Json" />

<div id="JsonResponse"></div>

<script language="javascript" type="text/javascript"> 

var data = [{ "lastName": "Orue", "firstName": "Esteban", "phones": [{ "type": "Mobile", "number": "(011) 4121-2121" },

{ "type": "Home", "number": "(011) 4123-4567"}]

},

{
"firstName": "Alejandro", "lastName": "Orue", "phones": [{ "type": "Mobile", "number": "(011) 4121-7777" },

{ "type": "Home", "number": "(011) 4121-9999"}]}];

 

function JsonInAction() {

   $.ajax({

    url:
'/Home/CollectJsonData',

    data: JSON.stringify(data),

    type: 'POST',

    contentType: 'application/json; charset=utf-8',

    dataType: 'json',

    success: function (result) {

     var divInsert = document.getElementById("JsonResponse");

     divInsert.innerHTML = result;

    },

    error: function () {  

   alert("error");

    }

   });

}

Controller:

 public ActionResult CollectJsonData(List<Person> person)

{

   return Json(data: person[0].firstName.ToString());

}

Model:

 public class Person

{

   public string firstName { get; set; }

   public string lastName { get; set; }

   public List<Phone> phones { get; set; }

}

 public class Phone

{

   public string type { get; set; }

   public string number { get; set; }

}

Note: Add a breakPoint in the ActionResult and see that you have 2 Persons, here is the magic of the Model Binder, MVC3 knows with the Model Binder that you have a class Person and you are sending in the post 2 persons.

Hope that Help!

Happy Coding Friends!!

Revision number 2, Tuesday, July 12, 2011 12:31:14 PM by evanorue

Comments

Related Articles

Security Guidelines and Recommendations

There's a great deal of good prescriptive security guidance out there in the form of whitepapers and books. Whitepapers patterns & practices Security Guidance for .NET Framework 2.0 patterns & practices ASP.NET 2.0 Security Guidance patterns &

javascript replace() function replace only single characters

javascript replace() function replace only single characters I have been trying to use javascript replace() to search and replace special individual characters in a string. But having some difficulties. We can use the following way to solve the following problem

F# Samples

Hi in this post only I want to help some links that will help you with F#. I think that F# is a good tool for different knid of things, so if you are working with that this can help you: http://fsharpsamples.codeplex.com/ http://fsharpnews.blogspot.com/2010

Enterprise Library

Enterprise Library "The patterns & practices Enterprise Library from Microsoft is a library of application blocks designed to assist developers with common enterprise development challenges. Application blocks are a type of guidance, provided as source

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. abiruban (1)