将多个JSON("行"或对象)传递给Controller

时间:2015-06-29 01:34:54

标签: jquery ajax json asp.net-mvc-4

var POS = {
        AddItem: function (SKU) {
            $.ajax({
                type: 'POST',
                data: '{"code":"itm-0008", "qty":"5"},
                       {"code":"itm-0009", "qty":"1"},
                       {"code":"l1", "qty":"8"}',
                url: '@Url.Action("AddProduct", "POS")',
                success: function (data) {
                   alert("Items Successfully Added!");
                },
                error: function (req, status, errorObj) {
                    alert(errorObj.toString());
                }
            });
        }
};

这是我的控制器:(它只能处理一(1)个JSON"行"或对象)

    [HttpPost]
    public void AddProduct(Item items)
    {
        Inventory inv = new Inventory();

        inv.Add(items.code, items.qty);
        inv.Dispose();
    }

我如何传递多个JSON" row"或反对控制器?

1 个答案:

答案 0 :(得分:1)

将您的json更改为对象数组:

[{"code":"itm-0008", "qty":"5"},
                   {"code":"itm-0009", "qty":"1"},
                   {"code":"l1", "qty":"8"}']

控制器:

for each(var item in items)
      inv.Add(item.code, item.qty);