小编use*_*582的帖子

从Javascript将KeyValuePair或IDictionary的列表传递给Web Api控制器

我有一个web api控制器,我想发布两个参数.一个是扁平的int ID,另一个是IDictionary或类似的等价物.

[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'
});
Run Code Online (Sandbox Code Playgroud)

无论我在数据参数(data: things,data: '=' + things)中尝试什么,字典都不会通过api控制器.它为null或有一个伪造条目({0,0}).

我也尝试在Uri中发送词典 - 不用了.

如何将字典或键值对发送到api控制器?

javascript c# ajax asp.net-web-api

13
推荐指数
1
解决办法
3万
查看次数

标签 统计

ajax ×1

asp.net-web-api ×1

c# ×1

javascript ×1