0

My ApiController method is returning JSON in this format:

"[{\"Category\":{\"CategoryType\":{\"Categories\":[{\"Client\":{\"Categories\":[{\"CategoryType\":{\"Categories\":[{\"Controls\":[],\"Risks\":[{\"Controls\":[],\"PCRMaps\":[],\"Id\":3,\"Title\":\"Risk with ID\",\"Description\":\"Test\",\"CategoryId\":80, etc etc

I have AngularJS code to render a drop-down list from this JSON object, as follows:

  <select ng-model="newRisk.ClientId" id="ddlClient" name="ddlClient">
     <option></option>
     <option ng-repeat="client in clients" value="{{client.Id}}">{{client.Name}}</option>
   </select>

But the error I'm getting seems to indicate that this JSON is being interpreted improperly:

Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. 
Use 'track by' expression to specify unique keys. 
Repeater: client in clients track by client.Id, 
Duplicate key: undefined, Duplicate value: "["

It's that last line that's throwing me off - duplicate value "["

Here's the controller code. Pretty straightforward:

       [HttpGet]
       public string GetAll()
       {

           return JsonConvert.SerializeObject(clientRepo.GetAll(),
                           Formatting.Indented,
                           new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });

       }

Do I need to take an extra formatting step before I return the JSON object?

KreepN
  • 8,528
  • 1
  • 40
  • 58
JMax2012
  • 373
  • 2
  • 6
  • 24
  • Looks like `clients` is not an object it is a string (json) instead. Also prefer using [ng-options](https://docs.angularjs.org/api/ng/directive/select) instead of ng-repeat for select. – PSL Dec 08 '14 at 17:44
  • Hm. It SHOULD be an object ... riskFactory.getClients().success(function (data) { $scope.clients = data; }) .error(function (data) { $scope.error = "An Error has occurred while loading clients! " + data.ExceptionMessage; }); – JMax2012 Dec 08 '14 at 17:49
  • I'll look at using ng-options instead, see if that helps at all – JMax2012 Dec 08 '14 at 17:50
  • Try `console.log(data)`. By doing `$scope.clients = data` does not ensure data is an object.. :) `Duplicate value: "["` seems like it is trying to repeat each character in the JSON – PSL Dec 08 '14 at 18:18
  • The JSON snippet I pasted above came from a console.log(data) call. I agree that it seems like each character is being repeated, I'm just not sure how to solve that. – JMax2012 Dec 08 '14 at 18:53
  • Yeah your server must be stringifying it twice.. Inorder to make it work (as a temporary hack) you could just quickly do `$scope.clients = JSON.parse(data)`. – PSL Dec 08 '14 at 18:57
  • Few things: not having the full json string makes it hard for us to parse, and understand the data structure. You could also show the class clientRepo so we understand what to expect when dealing with the serialization of those objects. – KreepN Dec 08 '14 at 19:31

1 Answers1

0
{
"entries": [{
    "id": 1,
    "name": "Partha",
    "niceName": "Sonu"
}, {
    "id": 2,
    "name": "David",
    "niceName": "Dav"
}]}

This type of format angular support for display data in ng-repeat

var data={"lists":angular.fromJson(response)};
                $scope.items =data.lists;