TaylorM
TaylorM

Reputation: 109

Having trouble passing a dictionary from jQuery to Controller

Controller:

    [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) {
                }
            });

But for some reason what is being passed is a dictionary with the key/value of Action and Controller:

From Visual Studio

Upvotes: 0

Views: 89

Answers (1)

Saman Gholami
Saman Gholami

Reputation: 3512

For a Dictionary you need to have both Key and Value properties, e.g.:

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

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

Then you can send data to the server:

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

Hope it helps

Upvotes: 1

Related Questions