Blake Rivell Blake Rivell - 3 months ago 20
Ajax Question

AJAX Put with json data to ASP.NET Web API method

I am attempting to do a simple AJAX PUT to my API method. The request is making it to the API method without any errors, but both parameters are not set. They are both 0. Why would this be?

AJAX Call:

var data = {
productId: 100,
oldIndex: 3
};

$.ajax({
url: '/api/products/reorder',
method: 'PUT',
data: JSON.stringify(data),
contentType: 'application/json'
});


API:

[HttpPut("api/products/reorder")]
public IActionResult ReOrder([FromBody]int productId, [FromBody]int oldIndex)
{
}

Joe Joe
Answer

I believe you should be able to do this:

    $.ajax({
        url: '/api/products/reorder/' + data.productId + '/' + data.oldIndex,
        method: 'PUT',
        contentType: 'application/json'
    });


    [HttpPut("api/products/reorder/{productId}/{oldIndex}")]
    public IActionResult ReOrder(int productId, int oldIndex)
    {
    }
Comments