将字典从jQuery传递到Controller时遇到问题

时间:2017-08-10 18:02:45

标签: c# jquery asp.net-mvc

控制器:

    [HttpPost]
    public void SubmitUserPicks(Dictionary<string,string> data)
    {

    }

Jquery的:

  var listOfPicks = [{gameID : "2", teamID : "3"}];
  $.ajax({
                type: "POST",
                traditional:true,
                url: "SubmitUserPicks",
                content: "application/json",
                dataType: "json",
                data: listOfPicks,
                success: function (result) {
                }
            });

但由于某种原因,传递的是具有Action和Controller的键/值的字典:

From Visual Studio

1 个答案:

答案 0 :(得分:1)

对于词典,您需要具有键和值属性,例如:

data['myDictionary[0].Key'] = "gameID";
data['myDictionary[0].Value'] = 2;

data['myDictionary[1].Key'] = "teamID ";
data['myDictionary[1].Value'] = 3;

然后您可以将data发送到服务器:

$.ajax({
        type: "POST",
        traditional:true,
        url: "SubmitUserPicks",
        content: "application/json",
        dataType: "json",
        data: data,
        success: function (result) {
        }
});

希望有所帮助