AspNetCore API ModelBinder 找不到值
AspNetCore API ModelBinder could not find a value
我正在尝试将一组对象(模型)传递给 http post 请求,但我的 API 控制器抛出错误:
Could not find a value in the request with name '' for binding
parameter 'list' of type 'SPMA.Models.Production.InProductionXML[]'.
wareList: InProductionXML[];
首先我从服务器获取数据:
getWareList(subOrder: subOrder) {
this.xmlService.getWareList(subOrder, 0)
.subscribe(response => {
this.wareList = response;
});
}
用户修改后,数据通过 post 请求发送回服务器。
exportWareList() {
this.xmlService.exportWareListToXML(this.wareList)
.subscribe(
response) => {},
(error) => {},
() => { }
);
在 xmlService 中,我正在调用 http post 请求:
exportWareListToXML(wareList: InProductionXML[]) {
return this.http.post('/api/xml/book/wareList', wareList);
}
最后在我的 XmlController 中,我想接收该列表:
[HttpPost("book/wareList")]
public IActionResult ExportWareList(InProductionXML[] list)
{
// do some work
}
我后端的模型:
namespace SPMA.Models.Production
{
public class InProductionXML
{
public string ComponentNumber { get; set; }
public string WareCode { get; set; }
public string WareName { get; set; }
public string WareUnit { get; set; }
public decimal? WareLength { get; set; }
public int ToIssue { get; set; }
public int Issued { get; set; } = 0;
public decimal? TotalToIssue { get; set; } = 0;
public decimal? Qavailable { get; set; } = 0;
public sbyte QCheckStatus { get; set; } = 0;
}
}
前端模型:
export class InProductionXML {
public componentNumber: string;
public wareCode: string;
public wareName: string;
public wareUnit: string;
public wareLength: number;
public toIssue: number;
public issued: number;
public totalToIssue: number;
public qAvailable: number;
public qCheckStatus: number;
constructor(componentNumber: string = null, wareCode: string = null, wareName: string = null,
wareUnit: string = null, wareLength: number = 0, toIssue: number = 0, issued: number = 0,
totalToIssue: number = 0, qAvailable: number = 0, qCheckStatus: number = 0) {
this.componentNumber = componentNumber;
this.wareCode = wareCode;
this.wareName = wareName;
this.wareUnit = wareUnit;
this.wareLength = wareLength;
this.toIssue = toIssue;
this.issued = issued;
this.totalToIssue = totalToIssue;
this.qAvailable = qAvailable;
this.qCheckStatus = qCheckStatus;
}
}
知道我做错了什么吗?
使用[FromBody]
属性,修改代码如下:
[HttpPost("book/wareList")]
public IActionResult ExportWareList([FromBody] InProductionXML[] list)
{
// do some work
}
[FromBody]
属性有什么作用?
[FromBody]
说不使用 url 而只使用正文。
我正在尝试将一组对象(模型)传递给 http post 请求,但我的 API 控制器抛出错误:
Could not find a value in the request with name '' for binding parameter 'list' of type 'SPMA.Models.Production.InProductionXML[]'.
wareList: InProductionXML[];
首先我从服务器获取数据:
getWareList(subOrder: subOrder) {
this.xmlService.getWareList(subOrder, 0)
.subscribe(response => {
this.wareList = response;
});
}
用户修改后,数据通过 post 请求发送回服务器。
exportWareList() {
this.xmlService.exportWareListToXML(this.wareList)
.subscribe(
response) => {},
(error) => {},
() => { }
);
在 xmlService 中,我正在调用 http post 请求:
exportWareListToXML(wareList: InProductionXML[]) {
return this.http.post('/api/xml/book/wareList', wareList);
}
最后在我的 XmlController 中,我想接收该列表:
[HttpPost("book/wareList")]
public IActionResult ExportWareList(InProductionXML[] list)
{
// do some work
}
我后端的模型:
namespace SPMA.Models.Production
{
public class InProductionXML
{
public string ComponentNumber { get; set; }
public string WareCode { get; set; }
public string WareName { get; set; }
public string WareUnit { get; set; }
public decimal? WareLength { get; set; }
public int ToIssue { get; set; }
public int Issued { get; set; } = 0;
public decimal? TotalToIssue { get; set; } = 0;
public decimal? Qavailable { get; set; } = 0;
public sbyte QCheckStatus { get; set; } = 0;
}
}
前端模型:
export class InProductionXML {
public componentNumber: string;
public wareCode: string;
public wareName: string;
public wareUnit: string;
public wareLength: number;
public toIssue: number;
public issued: number;
public totalToIssue: number;
public qAvailable: number;
public qCheckStatus: number;
constructor(componentNumber: string = null, wareCode: string = null, wareName: string = null,
wareUnit: string = null, wareLength: number = 0, toIssue: number = 0, issued: number = 0,
totalToIssue: number = 0, qAvailable: number = 0, qCheckStatus: number = 0) {
this.componentNumber = componentNumber;
this.wareCode = wareCode;
this.wareName = wareName;
this.wareUnit = wareUnit;
this.wareLength = wareLength;
this.toIssue = toIssue;
this.issued = issued;
this.totalToIssue = totalToIssue;
this.qAvailable = qAvailable;
this.qCheckStatus = qCheckStatus;
}
}
知道我做错了什么吗?
使用[FromBody]
属性,修改代码如下:
[HttpPost("book/wareList")]
public IActionResult ExportWareList([FromBody] InProductionXML[] list)
{
// do some work
}
[FromBody]
属性有什么作用?
[FromBody]
说不使用 url 而只使用正文。