3

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;
        }

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

1 Comment

If using Web API, add [FromUri] to the controller action parameter, otherwise you get binding errors.
1
$http(
  method: 'GET',
  url: ''Home/GetAllProjects'',
  params: {
    parentId: JSON.stringify(selectedIdArray )
  }
)

5 Comments

Hey Shiv , I am still getting null object in my MVC contrller method.
Hi, I have made a small edit. Previously I had set the param key to id now I have changed it to parentId. Did you try this ?
Thanks shiv , it worked.. Great ... but i am getting string as "[{\"id\":162},{\"id\":194}]" i need to parse this as i want get data based on 162 and 194 , how this can be done in c#
Use a regular expression that will extract the numbers out of your string.
"[{\"id\":162},{\"id\":194},{\"id\":295}]" i have this kind of string , using char array its making code complex , i want to extract these integers and need to put them in an array.

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.