I am trying to send jQquery object through ajax to a Web API controller.
Here's my jquery code:
var CurrentOrder = [];
var lineItem = { 'rowid': '34', 'quantity': '4', 'comment': 'somecomment' ,
'rowid': '22', 'quantity': '5', 'comment': 'somecomment1'};
CurrentOrder.push(lineItem);
$.ajax({
type: 'PUT',
data: JSON.stringify({ SessionCart: CurrentOrder }),
contentType: 'application/json; charset=utf-8',
url: '/api/Cart/UpdateCart/',
//traditional: true,
beforeSend: function () {
res.container.append(res.loader);
},
success: function () {
res.container.find(res.loader).remove();
$('.loader').attr('style', 'display:none');
GetSessionCart();
},
error: function () {
res.container.find(res.loader).remove();
$('.loader').attr('style', 'display:none');
alert('Current Order could not be updated. Please Try again.')
}
})
Here's my model:
public class Cart
{
public string rowid { get; set; }
public string quantity { get; set; }
public string comment { get; set; }
}
And the controller:
[HttpPut, HttpDelete]
[ActionName("UpdateCart")]
public HttpResponseMessage UpdateCart([FromUri] List<Cart> SessionCart)
{
//do sth
}
The problem is the controller gets hit but the parameter SessionCart count is always 0. I have tried not to stringify the object, using quotes to wrap the object, passing a single object and not an array and a bunch of other things. None of these worked. Any help would be much appreciated.