2

I'm having a problem by where I am posting an object to an MVC Core controller from a simple angularjs page.

The object at my MVC action is not binding although the object itself isn't null which is the usual problem with this.

Can anyone see what I am doing wrong?

This is my angular service code:

this.getQuote = function (priceRequest) {
    return $http.post('/quote/getcost', { priceRequest });
};

which is called by:

quoteService.getQuote(this.quoteData).then(function (cost) {
    $scope.quoteData.quoteCost = cost.data;
});

where this.quoteData is:

$scope.quoteData = {
                detailLevel: '0',
                fileLengthHours: 0,
                fileLengthMinutes: 1,
                excessiveSpeakersCount: 1,
                industry: null,
                deliveryTime: '1',
                additionalInformation: '',
                quoteCost: null
            };

This is the payload enter image description here

and this is the POST: enter image description here

Finally my C# MVC Core action:

[HttpPost]
public JsonResult GetCost([FromBody]PriceRequest priceRequest)
{
    var price = _priceCalculator.GetPrice(priceRequest);

    return new JsonResult(price);
} 

Although the object posted in is not null, none of the values have been bound: enter image description here

This is the PriceRequest object:

public class PriceRequest
{
    public JobDetailLevel DetailLevel { get; set; }

    public int FileLengthHours { get; set; }

    public int FileLengthMinutes { get; set; }

    public int? ExcessiveSpeakersCount { get; set; }

    public JobIndustry Industry { get; set; }

    public JobDeliveryTime DeliveryTime { get; set; }

    public string AdditionalInformation { get; set; }        
}

Can anyone see what I am doing wrong?

7
  • How is your .Net PriceRequest object defined? Commented Apr 4, 2017 at 14:28
  • Just added to the question Brad. Anything that isn't a string or int is an enum Commented Apr 4, 2017 at 14:30
  • Your screen shot of the "POST" is missing the body. What does the body look like? Commented Apr 4, 2017 at 14:34
  • The payload screenshot above it has it in? Commented Apr 4, 2017 at 14:34
  • priceRequest{DetailLevel: "0"... looks wrong, I'd expect something more like priceRequest{DetailLevel:{...etc}} basically your json doesn't seem correct Commented Apr 4, 2017 at 14:36

2 Answers 2

1

Ok so courtesy of this post: Asp.net core MVC post parameter always null

I needed to add this to my startup.cs:

.AddJsonOptions(jsonOptions =>
{
  jsonOptions.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
});

Thanks to those who tried to help.

Sign up to request clarification or add additional context in comments.

Comments

0

Try removing the { } from around the priceRequest variable on the angular side.

Like:

return $http.post('/quote/getcost', priceRequest);

2 Comments

My object at the server side becomes null doing this
It can't "become null". You're passing in a json object as a parameter to this function: this.getQuote = function (priceRequest) { } By wrapping priceRequest with { } you're wrapping your json object in another object so it isn't binding the body on the server side

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.