简体   繁体   中英

AspNetCore API ModelBinder could not find a value

I'm trying to pass an array of objects ( models ) to http post request but my API controller throws an error:

Could not find a value in the request with name '' for binding parameter 'list' of type 'SPMA.Models.Production.InProductionXML[]'.

wareList: InProductionXML[];

First I'm getting the data from the server:

getWareList(subOrder: subOrder) {
    this.xmlService.getWareList(subOrder, 0)
        .subscribe(response => {
            this.wareList = response;
        });
}

After modifying by the user the data is sent back to server with post request.

exportWareList() {
    this.xmlService.exportWareListToXML(this.wareList)
        .subscribe(
            response) => {},
            (error) => {},
            () => { }
        );

In xmlService I'm calling http post request:

exportWareListToXML(wareList: InProductionXML[]) {
        return this.http.post('/api/xml/book/wareList', wareList);
    }

Finally in my XmlController I want to receive that list:

[HttpPost("book/wareList")]
        public IActionResult ExportWareList(InProductionXML[] list)
        {
            // do some work
        }

Model in my backend:

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 in frontend:

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;
    }
}

Any ideas what am I doing wrong?

Use [FromBody] attribute, modify your code as following:

[HttpPost("book/wareList")]
        public IActionResult ExportWareList([FromBody] InProductionXML[] list)
        {
            // do some work
        }

What does [FromBody] attribute do?

the [FromBody] say to not use the url but only the body.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM