将jQuery键值对传递给控制器​​操作

时间:2018-05-07 12:01:04

标签: c# jquery asp.net asp.net-mvc dictionary

我有一个键值对,我把物品放在像这样的点击事件上:

var product_ids = {};
$(document).on('click', '.chkItem', function () {
    if ($(this).is(':checked')) {
        product_ids[$(this).attr('id')] = $(this).attr("price");
    } else {
        delete product_ids[$(this).attr('id')];
    }
});

通过这个我将ItemID值和项目的价格添加到键值对。

现在我需要将存储的这些值传递给我的控制器操作。我是这样做的:

$.post("/Controller/GetAllProducts", { ids: product_ids)},StartLoading())
    .done(function (data) {

    })
    .fail(function (data) {

     });

这是我的行动:

public async Task<JsonResult> GetAllProducts(Dictionary<string,string> ids) {
    //trying now to access te values...
}

当我检查ids字典的内容时,我得到以下值:

{[controller, myControllerName]}

{[action, GetAllProducts]}

我在这里做错了什么? :/

1 个答案:

答案 0 :(得分:-1)

您必须将表头或数据声明为表单,以便从c#中获取发布请求中的数据,您是否也检查了您在哪一方面的问题。尝试使用console.log处理您的json对象,然后检查来自c#

的传入数据