Posting array of JSON objects to MVC3 action method via JQuery Ajax

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.