asp.net - Passing complex JSON data jQuery to Action -
i have 2 classes used mirror data ajax call. 1 (customer) contains property name, , other array of products.
public class customer private _name string private _products product() public property name() string return _name end set(byval value string) _name = value end set end property public property products() product() return _products end set(byval value product()) _products= value end set end property
and ajax call:
$.ajax({ url: '../../customer/savecustomerdata', type: "post", datatype: "json", data: { "name": this.name, "products": [{ "productcode": "product 1", "productname": "product 1" }, { "productcode": "product 2", "productname": "product 2"}] }, success: function(data) { alert("customer has been saved!"); } });
the value customer.name reflected properties of products remain nothing while still having length of 2.
am missing important here?
right you're not passing json, you're passing data is, (serialized $.param()
)...which looks this:
name=something&products%5b0%5d%5bproductcode%5d=product+1&products%5b0%5d%5bproductname%5d=product+1&products%5b1%5d%5bproductcode%5d=product+2&products%5b1%5d%5bproductname%5d=product+2
to pass json, need stringify it, this:
data: json.stringify({ "name": this.name, "products": [{ "productcode": "product 1", "productname": "product 1" }, { "productcode": "product 2", "productname": "product 2"}] }),
which looks this:
{"name":"something","products":[{"productcode":"product 1","productname":"product 1"},{"productcode":"product 2","productname":"product 2"}]}
now that's model can turn object. older browsers (< ie8) don't support native json
, include json2.js
emulate behavior.
Comments
Post a Comment