如何在 C# .NET Web 中接收和拆分对象数组 API
How to receive and Split an array of objects in C# .NET Web API
我在前端有一个导入的 excel 文件,保存后,通过 [=] 将 excel 数据作为对象数组发送到我们的 .NET Web API
控制器14=],然后通过 DataTable
将此数据发送到 SQL stored procedure
。数组中的每个对象都是 excel 文件中的一行。
EDIT 我的数据是通过 POST
和 httpOptions
body:
从前端发送的。发送到控制器的数据如下所示:
[
{name: "Bob", position: "Software Developer", sales: 100000}
{name: "Ted", position: "Software Developer", sales: 100568}
{name: "George", position: "Software Developer", sales: 40000}
]
在C#
控制器中,如何接收这个包含未知数量对象的数组并使用它?目前我正在使用一个示例 forEach
块,它最初只接受一个 ID 数组,但这不起作用,因为现在我正在尝试发送一个对象数组,但我不能使用 .split
在上面:
[Route("api/myRoute")]
[HttpPost]
public async Task<IHttpActionResult> uploadExcelData
(
[FromBody] Array excelRows
)
{
string[] aryExcelRowsObjects;
DataTable dataTable = new DataTable();
dataTable.Columns.Add(new DataColumn("name", typeof(string)));
dataTable.Columns.Add(new DataColumn("position", typeof(string)));
dataTable.Columns.Add(new DataColumn("sales", typeof(int)));
// this is the example I'm using and it no longer works as you can't use .Split() on an array. What should I change it to?
aryExcelRowsObjects = excelRows.Split(',');
// this is the example I'm using and I don't think it accounts for adding multiple columns into a row
foreach (string s in aryExcelRowsObjects)
{
dataTable.Rows.Add(s);
}
}
我应该在代码注释的两个部分中更改什么?
您拥有的数据类型不一致,因此您现在发回的数组无法绑定到字符串,因此 excelRows
为空。因此,当您尝试拆分 excelRows
时,它会在您的脸上爆炸。
让您的控制器接受您发送的数据...
假设您要发回 Employee[]
。
public class Employee
{
public string Name { get; set; }
public string Position { get; set; }
public int Sales { get; set; }
}
现在,显然你不能在 Employee[]
上调用 string.Split
。但是您可以做 []
可用的任何事情,例如 for
、foreach
、Select
等等。
foreach(var employee in employees)
{
// do stuff...
}
我最近在处理相同的场景,您需要获取对象列表。
public class Employee
{
public string Name { get; set; }
public string Position { get; set; }
public int Sales { get; set; }
}
对于控制器
[Route("api/myRoute")]
[HttpPost]
public async Task<IHttpActionResult> uploadExcelData([FromBody] List<Employee> EmpList){
EmpList.ForEach(Emp=> {
...............Write your Code
}
我在前端有一个导入的 excel 文件,保存后,通过 [=] 将 excel 数据作为对象数组发送到我们的 .NET Web API
控制器14=],然后通过 DataTable
将此数据发送到 SQL stored procedure
。数组中的每个对象都是 excel 文件中的一行。
EDIT 我的数据是通过 POST
和 httpOptions
body:
从前端发送的。发送到控制器的数据如下所示:
[
{name: "Bob", position: "Software Developer", sales: 100000}
{name: "Ted", position: "Software Developer", sales: 100568}
{name: "George", position: "Software Developer", sales: 40000}
]
在C#
控制器中,如何接收这个包含未知数量对象的数组并使用它?目前我正在使用一个示例 forEach
块,它最初只接受一个 ID 数组,但这不起作用,因为现在我正在尝试发送一个对象数组,但我不能使用 .split
在上面:
[Route("api/myRoute")]
[HttpPost]
public async Task<IHttpActionResult> uploadExcelData
(
[FromBody] Array excelRows
)
{
string[] aryExcelRowsObjects;
DataTable dataTable = new DataTable();
dataTable.Columns.Add(new DataColumn("name", typeof(string)));
dataTable.Columns.Add(new DataColumn("position", typeof(string)));
dataTable.Columns.Add(new DataColumn("sales", typeof(int)));
// this is the example I'm using and it no longer works as you can't use .Split() on an array. What should I change it to?
aryExcelRowsObjects = excelRows.Split(',');
// this is the example I'm using and I don't think it accounts for adding multiple columns into a row
foreach (string s in aryExcelRowsObjects)
{
dataTable.Rows.Add(s);
}
}
我应该在代码注释的两个部分中更改什么?
您拥有的数据类型不一致,因此您现在发回的数组无法绑定到字符串,因此 excelRows
为空。因此,当您尝试拆分 excelRows
时,它会在您的脸上爆炸。
让您的控制器接受您发送的数据...
假设您要发回 Employee[]
。
public class Employee
{
public string Name { get; set; }
public string Position { get; set; }
public int Sales { get; set; }
}
现在,显然你不能在 Employee[]
上调用 string.Split
。但是您可以做 []
可用的任何事情,例如 for
、foreach
、Select
等等。
foreach(var employee in employees)
{
// do stuff...
}
我最近在处理相同的场景,您需要获取对象列表。
public class Employee
{
public string Name { get; set; }
public string Position { get; set; }
public int Sales { get; set; }
}
对于控制器
[Route("api/myRoute")]
[HttpPost]
public async Task<IHttpActionResult> uploadExcelData([FromBody] List<Employee> EmpList){
EmpList.ForEach(Emp=> {
...............Write your Code
}