[英]AspNetCore API ModelBinder could not find a value
我正在嘗試將對象數組(模型)傳遞給 http 發布請求,但我的 API controller 引發錯誤:
無法在名為“”的請求中找到類型為“SPMA.Models.Production.InProductionXML[]”的綁定參數“列表”的值。
wareList: InProductionXML[];
首先,我從服務器獲取數據:
getWareList(subOrder: subOrder) {
this.xmlService.getWareList(subOrder, 0)
.subscribe(response => {
this.wareList = response;
});
}
用戶修改后,數據通過發布請求發送回服務器。
exportWareList() {
this.xmlService.exportWareListToXML(this.wareList)
.subscribe(
response) => {},
(error) => {},
() => { }
);
在 xmlService 我調用 http 發布請求:
exportWareListToXML(wareList: InProductionXML[]) {
return this.http.post('/api/xml/book/wareList', wareList);
}
最后在我的 XmlController 中,我想收到該列表:
[HttpPost("book/wareList")]
public IActionResult ExportWareList(InProductionXML[] list)
{
// do some work
}
Model 在我的后端:
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;
}
}
Model 在前端:
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 而只使用身體。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.