2

I am sending an array of integers to the web api controller method successfully using angularjs $resource custom method without params defined. When I add other parameters with an array type parameter and define it in $resource custom method then other parameters are received successfully but the array found null. Here is the code with only array that is working fine. AngularJS Service

 var Product_Service = $resource('api/Product/:productId', { productId: '@id' },
   {
       'update': { method: 'PUT' },
       'bulkDelete': {
           method: 'POST',
           url: 'api/Product/BulkDelete',
           isArray:true
       }
    }

Calling it like this (passing directly the array):

bulkDelete: function (productIds) {
                var deferred = $q.defer();
                Product_Service.bulkDelete(productIds, function (response) {
                    deferred.resolve(response);
                }, function (response) {
                    deferred.reject(response);
                });
                return deferred.promise;
            },

Server side:

  [Route("BulkDelete")]
    public HttpResponseMessage BulkDelete(int[] ProductIds)
    {

        return Request.CreateResponse(HttpStatusCode.OK);
    }

Problem: I want to receive this array with another parameter but in that case array is received with value null.

  [Route("BulkDelete")]
    public HttpResponseMessage BulkDelete(int[] ProductIds,string hi)
    {
        //now ProductIds is null with another string hi parameter
        return Request.CreateResponse(HttpStatusCode.OK);
    }

angularjs service:

'bulkDelete': {
           method: 'POST',
           url: 'api/Product/BulkDelete',
           isArray:true,
           params:{
               productIds:'@productIds',
               hi:'@hi'
           }
       }

Now m calling like this,

bulkDelete: function (productIds) {
                var deferred = $q.defer();
                Product_Service.bulkDelete({productIds:productIds,hi:'hi how'}, function (response) {
                    deferred.resolve(response);
                }, function (response) {
                    deferred.reject(response);
                });
                return deferred.promise;
            },

Where is the problem and is that possible? If yes where I need to amend?

1 Answer 1

4

Tested and it's working

API Controller

[Route("api/Product/BulkDelete")]
[HttpPost]
 public HttpResponseMessage BulkDelete(string hi, int[] productIds)
 {
            //now ProductIds is null with another string hi parameter

            return Request.CreateResponse(HttpStatusCode.OK);
 }

RESOURCE

 var Product_Service = $resource('api/Product/:productId', { productId: '@id' },
   {
       'update': { method: 'PUT' },
       'bulkDelete': {
           method: 'POST',
           url: 'api/Product/BulkDelete',
           isArray:true,
           params:{

               hi:'@hi'
           }
       }
    }

CALL TO API

var productIds = [1, 2, 3, 4];
Product_Service.bulkDelete({ hi:'hello' },productIds, function (response) {
console.log(response) 
});
Sign up to request clarification or add additional context in comments.

Comments

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.