0

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.

6
  • Did you try it with an actual typed parameter instead of using "Object" on your controller? You most likely have a class called "PriceSchedule", try using that instead of "object" Commented Jul 19, 2017 at 11:52
  • Yes, I did it through PriceScheduleDTO class as well, but although controller hits, values are still null. Commented Jul 19, 2017 at 11:53
  • 1
    What does your payload object look like? Have you verified that it isn't null? Commented Jul 19, 2017 at 11:57
  • Well, here is the structure: ` public class PriceScheduleDTO { public int Price { get; set; } public int Deposit { get; set; } public string Plan { get; set; } }` Commented Jul 19, 2017 at 11:58
  • Is that structure similar to the structure of "payload"? Are you sure payload is not null or empty? Can you post your HTTP Request details? EDIT: Try using Fiddler to capture the HTTP POST request Commented Jul 19, 2017 at 12:00

1 Answer 1

1

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)
{ /
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this solution and I got "Unsupported Media Type" - not even controller been hit. Is there something that has to be manipulated inside the service?
Please check the request in the development tools of your browser if they are sent as content type application/json and if the json in the request body is well formatted. If the content cannot be parsed by the registered serializers (JSON is as far as I know registered as default, for XML you have to do additional configuration) ASP.NET Core quits processing without hitting your controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.