My ajax request is returning with success, but the data doesn't seem to be there. I know the json serialization works because if I do a query on the database and serialize that, the query results are properly returned. In the case below, all I get back is "[]".
Edit: I've also done other tests like try to extract a single piece of data from itemsInCart
, and it appears to be totally empty (which justifies the response I get).
Model:
public class ItemInCart
{
[Key]
public int ItemId { get; set; }
public virtual Variety variety { get; set; }
public int Quantity { get; set; }
public virtual InventoryItem inventoryItem { get; set; }
public double Price { get; set; }
public virtual Variety price { get; set; }
}
Controller:
[HttpGet]
public ActionResult completeSale(List<ItemInCart> itemsInCart)
{
var json = new JavaScriptSerializer().Serialize(itemsInCart);
return Json(json, JsonRequestBehavior.AllowGet);
}
Ajax:
$.ajax({
type: "GET",
url: "/" + current_controller + "/completeSale", // the method we are calling
contentType: "application/json; charset=utf-8",
dataType: "json",
data: { "itemsInCart": itemsInCart },
success: function (result) {
alert("success " + JSON.stringify(result));
},
error: function (result) {
alert("failed " + result);
}
});
Request URL (from developer tools):
http://localhost:52459/Sale/completeSale?itemsInCart=[{"ItemId":1,"Quantity":"1","Price":3.5}]