Does the model binder not suport arrays of JSON objects? The code below works when sending a single JSON domain object as part of the Ajax post. However, when sending a array of JSON domain objects, the action parameter is null.
VarDomains= [{ DomainName: ' Testt1 ', Price: ' 19.99 ', Available: True }, { DomainName: ' Testt2 ', Price: ' 15.99 ', Available: False }];$.Ajax({Type: ' POST ',Url: Url.Basketadddomain,DataType: "JSON", Data: Domains, Success: function ( Baskethtml { }, Error: function (a , B, C) { Alert ( ' A problem ocurred ' } });
This is the action method:
public ActionResult AddDomain(IEnumerable<DomainBasketItemModel> domain){ ...
Any ideas if it was possible to does this?
Answers
You need:
VarDomains= {Domains: [...Your elements...]};$.Ajax({Type: ' Post ',Url: ' Your-uri ' , Data: JSON stringify (domains: , Traditional: , Success: function (data) { ... } });
In general, check out the Request object in the debugger, you'll see what ' s being passed and get a idea of how to "speak" HTTP.
Note: Just found this out. The model binder chokes on nullable decimal properties like:
public decimal? latitude { get; set; }
So it won ' t bind the If you ' re posting to it with a JSON string of looks like this:
{"latitude":12.0}
But it'll work if your post something like this:
{"latitude":"12.0"}
See:http://syper-blogger.blogspot.com/2011/07/hello-world.html
Source:http://stackoverflow.com/questions/6031206/posting-array-of-json-objects-to-mvc3-action-method-via-jquery-ajax
Posting array of JSON objects to MVC3 action method via JQuery Ajax