Project Description
Proper object serialization for jQuery to ASP.MVC ajax requests.

NuGet Package
You can get it using Visual Studio's NuGet Packet manager by typing netpost or jquery.netpost or under following link:
https://nuget.org/packages/jquery.netpost/

Example usage in ASP.MVC 3 Web Application
Please go to "Source Code" secition and browse to "netpost -> Scripts". There you will find full source code to netpost jQuery plugin.

Usage example
JavaScript
$.netpost({
	url: 'http://www.example.com/ArrayOfObjects/',
	data: [
		{ Id: 1, What: 'TV', Dimensions: [10, 20, 30] },
		{ Id: 2, What: 'Car', Dimensions: [120, 210, 160] }
	]
}).success(function (data) { alert(data.message); });

.NET
// Model
public class ArrayOfObjects
{
	public int Id { get; set; }
	public string What { get; set; }
	public List<int> Dimensions { get; set; }
}

// Controller's Action
public virtual JsonResult ArrayOfObjects(List<ArrayOfObjects> arrayOfObjects)
{
	// some code here
}


Reason for the project
To see why $.netpost serialization is useful please look at following examples.

Example 1: Object literal:

{ Id: 1, What: 'TV', Dimensions: [10, 20, 30] }

should be serialized to (and $.netpost does that)

Id:1
What:TV
Dimensions[0]:10
Dimensions[1]:20
Dimensions[2]:30

but jQuery alone serializes it to something that .NET does not recognise

Id:1
What:TV
Dimensions[]:10
Dimensions[]:20
Dimensions[]:30


Example 2: Object literal:

[
{ Id: 1, What: 'TV', Dimensions: [10, 20, 30] },
{ Id: 2, What: 'Car', Dimensions: [120, 210, 160] }
]

$.netpost serialization:

[0].Id:1
[0].What:TV
[0].Dimensions[0]:10
[0].Dimensions[1]:20
[0].Dimensions[2]:30
[1].Id:2
[1].What:Car
[1].Dimensions[0]:120
[1].Dimensions[1]:210
[1].Dimensions[2]:160

jQuery serialization
undefined:undefined
undefined:undefined


end of story :D

Last edited Jan 10, 2013 at 9:13 AM by przemek, version 6