Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将KeyValuePair或IDictionary列表从Javascript传递给Web Api控制器_C#_Javascript_Ajax_Asp.net Web Api - Fatal编程技术网

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

C# 将KeyValuePair或IDictionary列表从Javascript传递给Web Api控制器,c#,javascript,ajax,asp.net-web-api,C#,Javascript,Ajax,Asp.net 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 }) thin

我有一个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'
});
[HttpPost]
public void DoStuff(int-id,[FromBody]i字典内容)
{
}
var things=newarray();
事物。推({1:10});//尝试过的东西。推送({Key:1,Value:10})
事物。推({2:11});
$.ajax({
url:'/api/DoStuff?id='+id,
data:'='+JSON.stringify(things),//在这里尝试了100种不同的东西
键入:“POST”,
数据类型:“json”
});
无论我在数据参数(
data:things
data:'='+things
)中尝试了什么,字典都不会通过api控制器。它要么为null,要么有一个伪条目({0,0})

我也试着用Uri发送字典-不可以


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

字典类似于键值对数组。你必须发送

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

你的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'
    });
    public ActionResult DoStuff(int id, Dictionary<int, int> things)
    {
        // ...
    }
像这样的行动

    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'
    });
    public ActionResult DoStuff(int id, Dictionary<int, int> things)
    {
        // ...
    }
public-ActionResult-DoStuff(int-id,Dictionary-things)
{
// ...
}

您不需要数组-您需要一个简单的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'
});

这一个对我有用:

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

这确实很古老,但在JS中我创建了一个像@cesardaniel这样的对象:

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

但是我在.net中的对象形状是
dictionars类型的。我尝试过这个,但仍然不起作用。你能给我举一个完整的例子吗?从您的帖子中,我仍然不确定如何将您提供的这个数组提供给ajax调用的数据参数。我完全按照您的方式进行了尝试,但它不起作用。“事物”字典是空的。我看到您正在从控制器方法返回ActionResult。也许你没注意到我使用的是ApiController而不是常规的控制器?我认为这改变了发布内容的方式,因为它使用了Http.yup。。在这种情况下,它可能是双重的,这超出了我的理解,线程的OP如何认为该响应是答案。回答问题的人发布了一篇文章的链接,这篇文章是在微软发布Web Api之前3年写的。它不包含此问题的答案。这不起作用。Dictionary参数在ApiController中显示为null。在我的答案中,
data
参数的开头有一个额外的“=”(我只是编辑它以删除它)。拆下后,它应该可以工作。基本上,请求(尝试在fiddler或浏览器的开发工具中查看)主体应该是
{“1”:10,“2”:11}
。如果这就是您所拥有的,那么参数应该正确绑定到字典。是的,就是这样!非常感谢!我花了一些时间才把它弄好。我当时正在打一个GET电话[FromUri],直到我切换到POST[FromBody]才让它工作。然后它成功了!:-)这对我来说效果最好,尽管我还必须引用key和value,因为我是第一个JSON.parse'ing,所以我必须这样做:
settings.push(JSON.parse('{“key”:“settingX”,“value”:“valX”}))