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
You must Login to comment.
Revision #3
Mon, Feb 4, 2008 8:00 AM
by
|
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 &
|
New
Sun, Jul 8, 2012 1:05 PM
by
|
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
|
Revision #3
Sat, Jun 25, 2011 11:56 PM
by
|
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
|
Revision #2
Tue, Mar 18, 2008 9:50 AM
by
|
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
|