最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to send array with other parameters to a web api using angularjs - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question edited Sep 5, 2014 at 9:36 ahmadalibaloch asked Sep 5, 2014 at 7:36 ahmadalibalochahmadalibaloch 6,0213 gold badges53 silver badges61 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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) 
});
发布评论

评论列表(0)

  1. 暂无评论