0

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.

2 Answers 2

1

[FromUri] should either not be there at all or change it to [FromBody] -

[FromUri] will be looking at the querystring for the data vs the PUT body

Also, it looks like you should be setting your data to:

data: JSON.stringify(CurrentOrder),

This, plus what Matteo1010 suggests.

Sign up to request clarification or add additional context in comments.

1 Comment

You are a lifesaver. Thanks! the problem was [FromUri] & JSON.stringify. Have a good one :D
1

I think json is wrong:

var lineitem = { 'rowid': '34', 'quantity': '4', 'comment':     'somecomment'} ;
CurrentOrder.push(lineItem);
lineitem = { 'rowid': '22', 'quantity': '5', 'comment': 'somecomment1} ;
CurrentOrder.push(lineItem);



data: JSON.stringify(CurrentOrder), 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.