简体   繁体   English

我如何在数组中发布乘法对象并在action方法中获取数据

[英]How can i post multiply objects in array and get the data in the action method

I am using ASP.NET Core 2.1 with Web API and posting works fine when I send one object. 我正在将ASP.NET Core 2.1与Web API一起使用,并且当我发送一个对象时,投递工作正常。 How can I send this type here. 我如何在这里发送此类型。

Until now I have tried this approach, https://ibb.co/dKztDGv , but without success. 到目前为止,我还尝试过https://ibb.co/dKztDGv这种方法,但是没有成功。 I don't get an error but I get the values from the last object only. 我没有收到错误,但仅从最后一个对象获取值。 Please help me get all the values in the array. 请帮助我获取数组中的所有值。

//I post this type of objects in array 
[
 {something...},
 {something...}
]

//what I've tried
public IActionResult PostNewLanguages([FromBody] JObject newLanguages, string id)
{
    var oneUser = GetSpecificUser(id);
    JObject class1DataJson = newLanguages;
    return Ok();
}

When I run this and send an array of objects I get this error 当我运行它并发送对象数组时,出现此错误

InvalidCastException: Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'Newtonsoft.Json.Linq.JObject'. InvalidCastException:无法将类型为“ Newtonsoft.Json.Linq.JArray”的对象转换为类型为“ Newtonsoft.Json.Linq.JObject”的对象。

So I suggest trying to send something like this instead 所以我建议尝试发送类似这样的内容

{ "myData": [
    {something...},
    {something...}
]}

And then to access the data you will need to do 然后访问数据,您将需要执行

var myValues = newLanguages["myData"];

foreach(var value in myValues)
{
}

'myData' being whatever key you use for your array. “ myData”是您用于数组的任何键。 Looks like 'request' in your case going by your comments. 根据您的评论,您的情况看起来像是“请求”。

If you post an array of object, you need to receive it as a List like 如果您发布对象数组,则需要像列表一样接收它

public IActionResult PostNewLanguages([FromBody] List<JObject> newLanguages, string id)
{
    foreach(var obj in newLanguages)
    {

    }

    return Ok();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在动作方法中,如何将发布数据绑定到动态对象? - In an action method, how can I bind post data to a dynamic object? 如何通过Ajax POST将数据绑定到Action方法的两个复杂类型参数 - How can I bind data to two complex type parameters of Action method by Ajax POST 如何在控制器动作和发布方法之间获取数据 - How to get data between controller action and post method 如何使用 c# MVC 在操作方法中以奇数/偶数为基础获取备用数据? - How can i get alternate data on odd/even basis inside action method using c# MVC? 如何改进从用户配置文件对象中获取过滤数据的方法? - How can I improve my method to get filtered data from user profile objects? 如何获得当前控制器动作方法的相对Uri? - How can I get a relative Uri for current controller action method? 如何修改我的代码以从C#中的对象数组中获取数据? - How can I modify my code to get data from an array of objects in C#? 如何在post方法中获得实际的控制器 - How can i get the actual controller in post method 检测动作是 POST 还是 GET 方法 - Detect if action is a POST or GET method 如何在OnActionExecuted(ActionExecutedContext filterContext)内部访问在Action方法中创建的对象 - How can I access the Objects created in an Action Method inside OnActionExecuted(ActionExecutedContext filterContext)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM