0

I have an MVC action that expects an array of int (int[] ids). I'm using $http post of angularjs but everytime I post it would return a null

$http({
            method: 'POST',
            url: url,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8;' },
            data: $.param({
                pcode: $scope.pcode,
                bId: $scope.bId,
                brandIds: [898,55],
                regId: $scope.regId,
                __RequestVerificationToken: getVerificationToken() // this is Html.Antiforgery that the action needs for posts
            })
        }).success(function (result) {});

I tried this solution

AngularJs $http.post() does not send data

but it's throwing an injection error if i inject the $httpProvider. I'm using angularjs 1.3

Community
  • 1
  • 1
gdubs
  • 2,724
  • 9
  • 55
  • 102

1 Answers1

3
$http({
            method: 'POST',
            url: url,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8;' },
            data: $.param({
                pcode: $scope.pcode,
                bId: $scope.bId,
                brandIds: [898,55],

                regId: $scope.regId,
                __RequestVerificationToken: getVerificationToken() // this is Html.Antiforgery that the action needs for posts
            },true)
        }).success(function (result) {});

When you do not supply that parameter it will encode array brandids[]. You can see this in request.form. This is not working with mvc model binding.

If you supply that value then it encode array as brandids. This is what mvc model binder need so it will work.

dotnetstep
  • 17,065
  • 5
  • 54
  • 72