简体   繁体   中英

Angular4 http.post .NET Core MVC bind failure

Tried a lot of solutions to pass an object from Angular4 service to c# controller. Although I do have the object received in service, does not bind to c# controller and so, I receive null .

Angular service:

getShedule(payload: any) {
    this._http.post("Shedule/GetSchedule", payload)
        .map(res => res.json()).subscribe((x) => {
            console.log("callback succes");
        });
}

C# controller:

[HttpPost]
public void GetSchedule(object priceScheduleObject)
{ 
    //logic here
}

Any help is welcome.

Try to change your C# controller to

[HttpPost]
public void GetSchedule([FromBody] JObject priceScheduleObject)
{ /

The [FromBody] annotations let the ASP.NET Core Binding logic look into the body of the message (and not posted form fields). If your do not want to interact with the JObject representing the JSON data you can bind the data to a model like

public class PriceSchedule {
   public string Name {get; set;} // just an example, propert names depend on your json
   ...
}

[HttpPost]
public void GetSchedule([FromBody] PriceSchedule priceScheduleObject)
{ /

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