My controller as follow:
[HttpPost]
public JsonResult getCartFromClient(List<JSItem> data)
{
return view();
}
and My JS code to send Json as follow:
var list = JSON.parse(localStorage.getItem('cart'));
$.ajax({
url: "CheckOut/getCartFromClient/",
contentType: "application/json",
dataType: 'json',
type: "POST",
data: JSON.stringify(list)
});
Now the issue is , when the conttroller been called , data will contain the right number of items in the array, however the content of each JSItem is null , Id is null the name is null .. !?
not sure what causing this issue! I get the right number of objects in the list but the content is null?
The ViewModel is as follow:
public class JSItem
{
public string xid { get; set; }
public string xamount { get; set; }
public string xprice { get; set; }
public string xname { get; set; }
public string xlevel { get; set; }
//public string ximg { get; set; }
}
public class Rootobject
{
public JSItem JSItem { get; set; }
}
Json.stringify(list) will bring the following json file:
"[{"JSItem":{"xid":"2","xamount":"1","xprice":"50.00","xname":"BaBy Product 1","xlevel":"0"}},{"JSItem":{"xid":"3","xamount":"1","xprice":"0.00","xname":"BaBy Product 122","xlevel":"0"}}]"
var list = JSON.parse(localStorage.getItem('cart'));
return and what is yourJSItem
model?list
without doing a JSON.stringify ?List<JSItem> data
, thenlist
needs to bevar list = { data: [{ xid: '2', xamount: '1', xprice: '50.00', etc}, { etc }] };
(and why are you making your properties allstring
when they are clearlyint
,decimal
etc?)