$http post not binding with asp.net MVC model

All we need is an easy explanation of the problem, so here it is.

Why is the payload from a angularjs $http post not beeing binded to the input model?

When the action is called the model is null and the request.params and request.forms does not show any sign of a form beeing sent. But the fiddler request shows that the payload is beeing sent with JSON.

AngularJS:

$http({
            method: "POST",
            url: "price/add",
            data: {
                Id: $scope.id,
                StoreId: $scope.storeid,
                Name: $scope.name,
                Manufacturer: $scope.manufacturer,
                Price: $scope.price
            }
        })

Model:

public class PriceModel
    {
        public int? Id { get; set; }
        public int? StoreId { get; set; }
        public string Barcode { get; set; }
        public string Name { get; set; }
        public string Manufacturer { get; set; }
        public DateTime Created { get; set; }
        public double? Price { get; set; }
    }

controller and action method description

public class PriceController : Controller
    {
        [HttpPost]
        public int Add(PriceModel price)
        {

Fiddler:

POST http://localhost:4989/price/add HTTP/1.1
Host: localhost:4989
Connection: keep-alive
Content-Length: 70
Accept: application/json, text/plain, */*
Origin: http://localhost:4989
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://localhost:4989/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: nb,no;q=0.8,en-US;q=0.6,en;q=0.4

{"id":"","storeid":"","name":"asdf","manufacturer":"asdf","price":123}

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

I’m not sure if model binding is confused because the parameter is named price and you have a property in the PriceModel that’s also called Price. Can you try renaming the action parameter name?

Method 2

Just to save another person an hour, one general cause for this problem is when we use

http({
        url: url,
        method: 'POST',
        params: argument,
        headers: {
            'X-Requested-With': 'XMLHttpRequest'
        }
    }).success(function (data) {
        defered.resolve(data);
    }).error(function (data, status) {
        defered.reject(data);
    });

However if we change to the following works like a charm:

http.post(
            url,
            argument
        ).success(function (data) {
            defered.resolve(data);
        }).error(function (data, status) {
            defered.reject(data);
        });

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply