C# 使用 MVC Web API 发布对象数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16042853/
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
Posting array of objects with MVC Web API
提问by brimble2010
I have a basic post operation that works on a single object of RecordIem.
What I would like to do is do the same action but in bulk by posting an array of requests using the same format.
我有一个基本的后期操作,适用于RecordIem. 我想做的是通过发布使用相同格式的请求数组来批量执行相同的操作。
For instance:
例如:
public HttpResponseMessage Post(RecordItem request)
{
var recordItems = _recorder.RecordItem(request);
return Request.CreateResponse(HttpStatusCode.OK, recordItems);
}
And when I post the Json:
当我发布 Json 时:
{
Id : "7UP24fVkGOxSjrcclghe_mP2-po",
System : 1,
Environment : "Production"
}
everything works fine. I would like to post Json similar to:
一切正常。我想发布类似于以下内容的 Json:
{
Id : "7UP24fVkGOxSjrcclghe_mP2-po",
System : 1,
Environment : "Production"
},
{
Id : "ClPE188H4TeD2LbQPeV_EzCsKVM",
System : 1,
Environment : "Production",
Label : "RestTest1"
},
{
Id : "SAWTMJzm-_AFqoNw70-gLeUzB4k",
System : 1,
Environment : "Production"
}
And have a method similar to below pick this up:
并有一个类似于下面的方法来选择这个:
public HttpResponseMessage Post(RecordItem[] request)
{
var recordItems = _recorder.RecordItems(request);
return Request.CreateResponse(HttpStatusCode.OK, recordItems);
}
I've tried using both the [FromBody]and [ModelBinding]attributes on the array and tried using different types (List, IList, IEnumerable) but to no avail. When using [FromBody]the request parameter is nulland when using [ModelBinding]the list is empty. I've tried using both and that doesn't work either.
我尝试在数组上同时使用[FromBody]和[ModelBinding]属性,并尝试使用不同的类型(List、IList、IEnumerable)但无济于事。使用时[FromBody]请求参数是null,使用时[ModelBinding]列表为空。我试过同时使用两者,但也不起作用。
I'd rather not have to having to resort to looping single posts in my client.
我宁愿不必在我的客户端中循环播放单个帖子。
Thanks
谢谢
采纳答案by Maggie Ying
Since your Postexpects an RecordItem[], your JSON content in your request body should be in an array as well.
由于您Post期望的是RecordItem[],因此您的请求正文中的 JSON 内容也应该在一个数组中。
What you have is pretty close -- try adding a pair of square bracket []around your data:
您所拥有的非常接近 - 尝试[]在您的数据周围添加一对方括号:
[{
Id : "7UP24fVkGOxSjrcclghe_mP2-po",
System : 1,
Environment : "Production"
},
{
Id : "ClPE188H4TeD2LbQPeV_EzCsKVM",
System : 1,
Environment : "Production",
Label : "RestTest1"
},
{
Id : "SAWTMJzm-_AFqoNw70-gLeUzB4k",
System : 1,
Environment : "Production"
}]
回答by Rob
It's important that your json contains the requestparameter name.
A other note: you have to post it as an array.
您的 json 包含request参数名称很重要。另一个注意事项:您必须将其作为数组发布。
Your json would look like this:
你的 json 看起来像这样:
{
"request": [
{
"Id": "...",
"System": 1,
...
},
{ ... }
]
}
回答by Oliver K?tter
For all that just get an empty array whatever they try, try this:
对于所有的尝试,无论他们尝试什么都只会得到一个空数组,请尝试以下操作:
var request = $.ajax({
dataType: "json",
url: "/api/users",
method: "POST",
data: { '': postData}
});
The data must be a single anonymous object instead of a raw array.
数据必须是单个匿名对象而不是原始数组。
Info was found here.
信息是在这里找到的。

