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

javascript - How to pass array as parameter to $http.get in Angularjs - Stack Overflow

programmeradmin0浏览0评论

Problem Statement

I have a javascript array which i want to pass as a parmeter to $http.get in AngularJs. This array will be passed to action method in MVC. what should be the syntax? Please Help me. I am stuck here. The array being passed is javascript array

Angular Directive

$scope.selectedIdArray = [];   
$scope.selectedIdArray.push({ id: $scope.selectedId })

$scope.$parent.getProjects($scope.selectedIdArray);

Angular Controller

$scope.getProjects = function (selectedIdArray) {
        $http.get('Home/GetAllProjects', { params: { "parentId[]": selectedIdArray } })
            .then(function (data) {
                    $scope.projects = [];
            angular.forEach(data.data, function (value, index) {
                $scope.projects.push({ id: value.N_LevelID, label: value.T_LevelName }
                );
            });
            })
        .catch(function (data) {
            console.error('Gists error', data.status, data.data);
        })
    }

MVC Controller Action Method

public JsonResult GetAllProjects(int?[] parentId = null)
        {
            iMetricsEntities dbcontext = new iMetricsEntities();
            JsonResult jr = new JsonResult();
            if (parentId == null)
            {
                jr.Data = dbcontext.Levels.Where(objelevel => objelevel.N_LevelTypeID == 2 && objelevel.B_Active);
                jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

                //return new JsonResult
                //{
                //    Data = dbcontext.Levels.Where(objelevel => objelevel.N_LevelTypeID == 2 && objelevel.B_Active),
                //    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                //};
            }
            else if (parentId != null)
            {
                foreach (var id in parentId)
                {
                    jr.Data = dbcontext.Levels.Where(objelevel => objelevel.N_LevelTypeID == 2 && objelevel.B_Active && objelevel.N_ParentID == id);
                    jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                }
            }

            return jr;
        }

Problem Statement

I have a javascript array which i want to pass as a parmeter to $http.get in AngularJs. This array will be passed to action method in MVC. what should be the syntax? Please Help me. I am stuck here. The array being passed is javascript array

Angular Directive

$scope.selectedIdArray = [];   
$scope.selectedIdArray.push({ id: $scope.selectedId })

$scope.$parent.getProjects($scope.selectedIdArray);

Angular Controller

$scope.getProjects = function (selectedIdArray) {
        $http.get('Home/GetAllProjects', { params: { "parentId[]": selectedIdArray } })
            .then(function (data) {
                    $scope.projects = [];
            angular.forEach(data.data, function (value, index) {
                $scope.projects.push({ id: value.N_LevelID, label: value.T_LevelName }
                );
            });
            })
        .catch(function (data) {
            console.error('Gists error', data.status, data.data);
        })
    }

MVC Controller Action Method

public JsonResult GetAllProjects(int?[] parentId = null)
        {
            iMetricsEntities dbcontext = new iMetricsEntities();
            JsonResult jr = new JsonResult();
            if (parentId == null)
            {
                jr.Data = dbcontext.Levels.Where(objelevel => objelevel.N_LevelTypeID == 2 && objelevel.B_Active);
                jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

                //return new JsonResult
                //{
                //    Data = dbcontext.Levels.Where(objelevel => objelevel.N_LevelTypeID == 2 && objelevel.B_Active),
                //    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                //};
            }
            else if (parentId != null)
            {
                foreach (var id in parentId)
                {
                    jr.Data = dbcontext.Levels.Where(objelevel => objelevel.N_LevelTypeID == 2 && objelevel.B_Active && objelevel.N_ParentID == id);
                    jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                }
            }

            return jr;
        }
Share Improve this question edited May 13, 2016 at 15:04 Navin Rane asked May 13, 2016 at 13:33 Navin RaneNavin Rane 311 gold badge1 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

If you define your controller action like this:

    public JsonResult GetAllProjects(int[] parentId)
    {
        // Stuff goes here
    }

You can call it like this:

    $http.get('Home/GetAllProjects', { params: { "parentId": [1, 2, 3, 42] } })
    .then(function(response) {
        // stuff goes here
    });

The reason this works is because query strings can be specified multiple times. Both $http and MVC interpret these as arrays.

In the above example, this is the URL that gets generated by $http, which the controller action model binds to an array:

http://localhost:56976/Home/GetAllProjects?parentId=1&parentId=2&parentId=3&parentId=42
$http(
  method: 'GET',
  url: ''Home/GetAllProjects'',
  params: {
    parentId: JSON.stringify(selectedIdArray )
  }
)
发布评论

评论列表(0)

  1. 暂无评论