1

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. The solution & the tip. Both worked.

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.