17

I have a web api controller to which I would like to post two parameters. One is a flat int ID, and the other is an IDictionary or similar equivalent.

[HttpPost]
public void DoStuff(int id, [FromBody]IDictionary<int, int> things)
{
}

var things = new Array();
things.push({ 1: 10 }); // tried things.push({ Key: 1, Value: 10 })
things.push({ 2: 11 });
$.ajax({
    url: '/api/DoStuff?id=' + id,
    data: '=' + JSON.stringify(things), // tried what seems like 100 different things here
    type: 'POST',
    dataType: 'json'
});

No matter what I try in the data param (data: things, data: '=' + things), the Dictionary does not come through to the api controller. It is either null or has one bogus entry ({0, 0}).

I have also tried to send the Dictionary in the Uri - no go.

How can I send the dictionary or key value pair to the api controller?

4 Answers 4

24

You don't need an array - you need a simple JS object (which maps to a dictionary). This code should work:

var things = {};
things['1'] = 10;
things['2'] = 11;
$.ajax({
    url: '/api/DoStuff?id=' + id,
    data: JSON.stringify(things),
    contentType: 'application/json'
    type: 'POST',
    dataType: 'json'
});
3
  • 1
    This does not work. The Dictionary parameter comes through as null in the ApiController. Commented May 14, 2013 at 22:54
  • 2
    There was an extra '=' from the start of the data parameter in my answer (I just edited it to remove it). After removing it, it should work. Basically, the request (try seeing it in either fiddler or the developer tools of your browser) body should be {"1":10,"2":11}. If that's what you have, then the parameter should be correctly bound to the dictionary. Commented May 14, 2013 at 23:00
  • 2
    It took me some time to get this working. I was making a GET call [FromUri] and couldn't get it to work until I switched to POST [FromBody]. Then it worked! :-)
    – James
    Commented Nov 23, 2016 at 21:59
4

This one worked for me:

var settings = [];
settings.push({ key: "setting01", value: "Value01" });
settings.push({ key: "setting02", value: "Value02" });
1
  • This worked best for me, though I also had to quote key and value because I was first JSON.parse'ing, so I had to do this: settings.push(JSON.parse('{"key": "settingX", "value": "valX"}'));
    – Suamere
    Commented Oct 4, 2017 at 14:29
0

This is really old, but in JS I created an object like @cesardaniel:

var dictionary = [];
dictionary.push({ key: 1, value: [1,2,3,4,5] });
dictionary.push({ key: 2, value: [6,7,8,9,0] });

but my object shape in .net was of type Dictionary<KeyValuePair<int, IEnumerable<int>>. The web api was then able to pick it up. Hope this helps a future reader!

-1

dictionary is like an array of key-value pairs. you have to send

var data = {}
data['things[0].Key'] = x
data['things[0].Value'] = y
// etc

EDIT:

your js should look something like this

    var data = { };
    data['things[0].Key'] = 1;
    data['things[0].Value'] = 1;
    data['things[1].Key'] = 22;
    data['things[1].Value'] = 12;

    $.ajax({
        url: /api/DoStuff?id=' + id,
        data: data,
        type: 'POST',
        dataType: 'json'
    });

and action something like this

    public ActionResult DoStuff(int id, Dictionary<int, int> things)
    {
        // ...
    }
4
  • I tried this, but it still doesn't work. Can you possibly post a complete example that I can try? I'm still not sure from your post how to supply this array you've provided to the data parameter of the ajax call. Commented May 14, 2013 at 22:17
  • I tried it exactly as you have it, and it does not work. The 'things' dictionary is empty. I see that you are returning ActionResult from your controller method. Perhaps you missed that I am using an ApiController and not a regular Controller? I think that changes the way things are posted since it uses Http. Commented May 14, 2013 at 22:31
  • yup.. in this case it is possible duplicte of stackoverflow.com/questions/11950351/…
    – maxlego
    Commented May 14, 2013 at 22:47
  • 1
    It's beyond my understanding how the OP of that thread considered that response the answer. The person who answered posted a link to an article that was written 3 years before the Web Api was even released by Microsoft. It does not contain the answer to this issue. Commented May 14, 2013 at 22:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.