ApiController破坏JSON,抛出Angular ng-repeater错误

时间:2014-12-08 17:41:20

标签: json angularjs asp.net-apicontroller

我的ApiController方法以这种格式返回JSON:

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

我有AngularJS代码来渲染此JSON对象的下拉列表,如下所示:

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

但是我得到的错误似乎表明这个JSON被解释得不正确:

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: "["

这是让我失望的最后一行 - 重复值“[”

这是控制器代码。非常简单:

       [HttpGet]
       public string GetAll()
       {

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

       }

在返回JSON对象之前,是否需要执行额外的格式化步骤?

1 个答案:

答案 0 :(得分:0)

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

这种格式角度支持ng-repeat中的显示数据

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