0

First of all, I'm not using MVC but only it's routing and controller (in order to create RESTful API). I'm using c# web form

The problem that I have is posting list of object using AJAX post to my api controllers. I've read several examples and tried their example to no anvil =( one of the example that I tried is this:

Passing A List Of Objects Into An MVC Controller Method Using jQuery Ajax

and

MVC Send list through AJAX

the data that I get is null, even I tried adding traditional: true in AJAX, still has no luck. Maybe someone can give me some insight. Here's my code:

javascript:

var data = { warehouseProduct: [] };
        data.warehouseProduct.push({
            PID: 2,
            PIDN: 'ABC',
            CName: 'Toy',
            EName: 'AKE-14',
            Qty: 4,
        });

        $.ajax({
            url: "/api/warehouse/PostUpdateData",
            type: "POST",
            contentType: 'application/json; charset=utf-8',
            async: false,
            dataType: "json",
            traditional: true,
            data: JSON.stringify(data),
            error: function (xhr, ajaxOptions, thrownError)
            {
                alert(thrownError);
            }
        }).done(function (msg)
        {
        });

here's the code in the controller:

public string PostUpdateData(List<Warehouse> warehouseProduct)
{
    // do something here
    return "";
}
Community
  • 1
  • 1
Jim
  • 1,123
  • 13
  • 29

1 Answers1

2

You wrapped the list of Warehouses in an object. You instead want to post just the array of Warehouses.

Try this:

var data = [];
data.push({
    PID: 2,
    PIDN: 'ABC',
    CName: 'Toy',
    EName: 'AKE-14',
    Qty: 4,
});
schaefea
  • 146
  • 1
  • 5