pavany
pavany

Reputation: 219

passing Array data dynamically in URL to call webapi method (C#)

below is my json generated data from the backend

{"langs":[{"cisAreaId":100,"area":"Prog","name":"C#"},{"cisAreaId":110,"area":"Prog","name":"Java"},{"cisAreaId":120,"area":"Prog","name":"MS.NET languages(VB.NET,etc)"},{"cisAreaId":130,"area":"Prog","name":"MS Visual Languages (VB, C++, etc)"},{"cisAreaId":140,"area":"Prog","name":"Python"},{"cisAreaId":150,"area":"Prog","name":"Ruby"}]}

above data i copied to $scope.areas. Now in my view

<div ng-repeat="lang in areas.langs">
        <label><input type="checkbox" ng-model="lang.selected" value="{{lang.cisAreaId}}" /> {{lang.name}}</label>
    </div>

once user click the submit, I need to capture selected checkbox values and send it as JSON data.

    $scope.languageArray = [];
    angular.forEach($scope.areas.langs, function (lang) {
        if (lang.selected) $scope.languageArray.push({ "cisAreaId": lang.cisAreaId });
    });
    var data = JSON.stringify({
        languages: $scope.languageArray,
    });

it is forming the array like after selecting the two of the checkbox values

{"languages":[{"cisAreaId":110},{"cisAreaId":120}]}

From the above code how can I pass the above dynamic array in the URL to call the backend method

Angularjs Controller Code:    
XXXService.step2Form(data).then(function (results) {
            console.log(results.data); 
        }, function (error) {
            alert(error.data.message);
        });

service code:
var _step2Form = function (data) {
       return $http.post(serviceBase + 'api/feedback/Step2Data' + data , { headers: { 'Content-Type': 'application/json' } });
        };

I have pass the data dynamically to the backend

public HttpResponseMessage Step2Data([FromBody] int[] languages)

but i am getting the error like this

http://localhost:53401/api/XXXX/Step2Data[object%20Object],[object%20Object] 404 (Not Found)

can anyone please tell me how to pass the array values dynamically in the url to call the webapi method.

Upvotes: 1

Views: 887

Answers (1)

Mustapha Larhrouch
Mustapha Larhrouch

Reputation: 3403

in $scope.languageArray you need to push just the id :

$scope.languageArray = [];
angular.forEach($scope.areas.langs, function (lang) {
   if (lang.selected) $scope.languageArray.push(lang.cisAreaId);
});
var data = JSON.stringify({
     languages: $scope.languageArray,
});

Upvotes: 2

Related Questions