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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16553561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:19:35  来源:igfitidea点击:

Passing List of KeyValuePair or IDictionary to Web Api Controller from Javascript

c#javascriptajaxasp.net-web-api

提问by user1810582

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.

我有一个 web api 控制器,我想向其发布两个参数。一个是 flat 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'
});

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}).

无论我在数据参数 ( data: things, data: '=' + things) 中尝试什么,字典都不会到达 api 控制器。它要么为空,要么有一个虚假条目 ({0, 0})。

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

我还尝试在 Uri 中发送字典 - 不行。

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

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

采纳答案by carlosfigueira

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

您不需要数组 - 您需要一个简单的 JS 对象(映射到字典)。此代码应该工作:

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'
});

回答by maxlego

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

你的 js 应该是这样的

    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)
    {
        // ...
    }

回答by Cesar Daniel

This one worked for me:

这个对我有用:

var settings = [];
settings.push({ key: "setting01", value: "Value01" });
settings.push({ key: "setting02", value: "Value02" });

回答by cr1pto

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

这真的很旧,但在 JS 中我创建了一个像 @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!

但我在 .net 中的对象形状是Dictionary<KeyValuePair<int, IEnumerable<int>>. 然后 Web api 就能够获取它。希望这对未来的读者有所帮助!