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?

Simon
  • 1,966
  • 5
  • 21
  • 35
  • How is your .Net PriceRequest object defined? – brad Apr 04 '17 at 14:28
  • Just added to the question Brad. Anything that isn't a string or int is an enum – Simon Apr 04 '17 at 14:30
  • Your screen shot of the "POST" is missing the body. What does the body look like? – Liam Apr 04 '17 at 14:34
  • The payload screenshot above it has it in? – Simon Apr 04 '17 at 14:34
  • `priceRequest{DetailLevel: "0"...` looks wrong, I'd expect something more like `priceRequest{DetailLevel:{...etc}}` basically your json doesn't seem correct – Liam Apr 04 '17 at 14:36
  • Do you have `CamelCasePropertyNamesContractResolver` added in your `Startup.cs`? If not, you will either need to add it or change names of your models and their properties to pascal case. – Emin Laletovic Apr 04 '17 at 14:37
  • eminlala I have added with no joy still:services.AddMvc() .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver()); – Simon Apr 04 '17 at 14:42

2 Answers2

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.

Community
  • 1
  • 1
Simon
  • 1,966
  • 5
  • 21
  • 35
0

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

Like:

return $http.post('/quote/getcost', priceRequest);
brad
  • 529
  • 4
  • 5
  • My object at the server side becomes null doing this – Simon Apr 04 '17 at 14:41
  • 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 – brad Apr 04 '17 at 14:43