sukesh
sukesh

Reputation: 2437

Access multiple JSON objects returned from C#

There are 2 dropdownlists on page and each is bound with data from DB. I plan to return the required 2 JSON objects from server side in a single trip. This is in order not to hit the database a second time.

How to access/use these returned objects is my question. Is this possible?

ASPX:

<select id="ddlSet">
 <option ng-repeat="item in Sets" value="{{item}}">{{item}}</option>
</select>
<select id="ddlCircle">
 <option ng-repeat="item in Circles" value="{{item}}">{{item}}</option>
</select>

C#:

[WebMethod]
public void GetReportFilters()
 {
   List<Set> lstSets = null;
   List<Circle> lstCircles = null;
   ReportsDB.GetFiltersData(out lstSets, out lstCircles);

   ReportFilters obj = new ReportFilters();
   obj.lstSets = lstSets;
   obj.lstCircles = lstCircles;
   JavaScriptSerializer serializer = new JavaScriptSerializer();
   Context.Response.Clear();
   Context.Response.ContentType = "application/json";
   HttpContext.Current.Response.Write(
          (serializer.Serialize(serializer.Serialize(obj))));

 }

If it was a single JSON object, I would do:

JS:

$scope.Sets=[];
$scope.Circles=[];
var promise = ReportsFactory.GetReportFilters();
        promise.then(function (success) {
            if (success.data != null && success.data != '') {
                $scope.Sets = JSON.parse(success.data);
            }
            else {
                console.log(success.data);
            }
        },
        function (error) {
            console.log(error);
        })

Upvotes: 1

Views: 425

Answers (1)

Icycool
Icycool

Reputation: 7179

Actually it IS a single JSON object, containing 2 other objects

if (success.data != null && success.data != '') {
    var data = JSON.parse(success.data);
    $scope.Sets = data.lstSets;
    $scope.Circles = data.lstCircles;
}

Edit: I think if you can cut 1 serialize on C# side, you can access the object on javascript without JSON.parse.

Upvotes: 2

Related Questions