[{prodId:'10',qnty:'12',total:'1200'},
{prodId:'11',qnty:'2',total:'10'},
{prodId:'4',qnty:'10',total:'50'}]
i have the following class
public class ListItem{
public int prodID {get;set;}
public int qnty {get;set;}
public decimal total {get;set;}
}
the above json array will be sent from ajax call to an action method. In the action method i need to build a List<ListItem>
collection from the json array. How do i do this?
UPDATES here is the controller
public class ShoppingCartController : Controller
{
public JsonResult AddToShoppingCart(string jsonString)
{
int carId = 0;
string[] str= jsonString.Split(',');
for (int i = 0; i < str.Length; i++)
{
if (str[i] == "cartId")
{
string tmp = str[i].Split(':').LastOrDefault();
carId = int.Parse(tmp);
if (carId == -1)
{
//create new cart
}
else {
}
}
}
}
Here is the ajax:
$('#addToCartForm #add').on('click', function () {
$.ajax({
url: 'ShoppingCart/AddToShoppingCart',
method: 'post',
data: JSON.stringify(item),
dataType: 'json',
success: function (data) {
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
});