c# - Passing List of KeyValuePair or IDictionary to Web Api Controller from Javascript -
i have web api controller post 2 parameters. 1 flat int id, , other 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 seems 100 different things here type: 'post', datatype: 'json' });
no matter try in data param (data: things
, data: '=' + things
), dictionary not come through api controller. either null or has 1 bogus entry ({0, 0}).
i have tried send dictionary in uri - no go.
how can send dictionary or key value pair api controller?
you don't need array - need simple js object (which maps dictionary). 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' });
Comments
Post a Comment