简体   繁体   中英

pass collection of objects through http post in angular js

I have pass a collection of objects through http post in angular js.

The code is as follows:

  $scope.selectedContent = function () {       
        var contents = $filter('filter')($scope.data.ContentId, { Selected: true });  // I could able to get all the selected objects here, No problem with it        
        var jsonData = angular.toJson(contents); //It is not able to convert to Json if there are more than 5 records
        var promise = $http.post('/webapi/cmsApi/CmsPublishApprovedContent?jsonData=' + jsonData, {});
        promise.success(function () {            
            window.location.reload();
        });



        [ReferrerFilterAttribute]
        [HttpPost]
        [System.Web.Http.ActionName("CmsPublishApprovedContent")]
        public void CmsPublishApprovedContent(string jsonData)
        {
            var contents = JsonConvert.DeserializeObject<List<ContentNodeInWorkFlow>>(jsonData);

            foreach (var content in contents)
            {
                _contentService.PublishContent(content.ContentId,  userId);
            }       
        }
    }

The above code works fine if there are 5 records or less. If there are more records, I could able to get all the selected record objects in the variable 'contents'. But the problem is occuring when converting to Json for all those objects. I have about 500 records to pass through. How can do I it?

There is no specific reason to convert to JSON data. I just need to extract the ids of all the selected items. I have modified the above code as below:

$scope.selectedContent = function () {        
        var contents = $filter('filter')($scope.data, { Selected: true });
        var abc = [];
        angular.forEach(contents, function(content)
        {
            abc.push(content.ContentId); // got all the ids in the array now
        });

        var promise = $http.post('/webapi/cmsApi/CmsPublishApprovedContent' ,{contents : abc});
        promise.success(function () {
            window.location.reload();
        });
    }

I have just took an array and pushed all the content ids into it. I could able to see all the ids in the array now. I tried to pass the array as above. How to retrieve those array in the code behind.

[ReferrerFilterAttribute]
        [HttpPost]
        [System.Web.Http.ActionName("CmsPublishApprovedContent")]
        public void CmsPublishApprovedContent(int[] abc)
        {}

I do not see any values obtained under int[] abc. What will be the datatype for the parameter in the method call above.

You need second argument of $http.post method. You have to send such data by POST requests, not in query of url. You can put some data into body of the post request.

You need this:

var postBodyWithHugeAmountOFData = {data: [1,2,3,4,5...500]};
$http.post(url, postBodyWithHugeAmountOFData).success(function () {});

Also, you must be ready to handle this request in your backend.

is there any specific reason u want to pass this data as a JSON?.

if ur using Web API in that case u can pass the object as it is but only make sure that collection in web API method contains all the property in javascript collection

Thank you for all your posts. It's working fine without converting to Json. The code is as below.

$scope.selectedContent = function () {        
        var contents = $filter('filter')($scope.data, { Selected: true });
        var promise = $http.post('/webapi/cmsApi/CmsPublishApprovedContent' ,contents);
        promise.success(function () {
            window.location.reload();
        });
    }

and the signature would be

public void CmsPublishApprovedContent(List<ContentNodeInWorkFlow> abc)
        {
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM