2016-11-11 5 views
0

まず、ここで私は取得していますエラーに私をリードし、すべてのコードです:JSONは角度/ MVC重複はリピーターで許可されていない

[ 
    { 
    "Id": 0, 
    "UserName": "uniqueusername", 
    "Photo": "base64string", 
    "Email": "[email protected]", 
    "Office": "Location " 
    }, 
    { 
    "Id": 1, 
    "UserName": "uniqueusername", 
    "Photo": "base64string", 
    "Email": "[email protected]", 
    "Office": "Location" 
    }, 
    { 
    "Id": 2, 
    "UserName": "uniqueusername", 
    "Photo": "base64string", 
    "Email": "[email protected]", 
    "Office": "Location" 
    }, 
    { 
    "Id": 3, 
    "UserName": "uniqueusername", 
    "Photo": "base64string", 
    "Email": "[email protected]", 
    "Office": "Location" 
    }, 
    { 
    "Id": 4, 
    "UserName": "uniqueusername", 
    "Photo": "base64string", 
    "Email": "[email protected]", 
    "Office": "Location" 
    } 
] 

それは、この機能を使用して生成されるが私のコントローラで:

List<string> Names = arepo.GetAllAvionteUsers(); 
List<UserPreviewViewModel> AllUsers = new List<UserPreviewViewModel>(); 
int count = 0; 
foreach(string name in Names) 
{ 

    UserPreviewViewModel preview = new UserPreviewViewModel(name); 
    preview.Id = count; 
    AllUsers.Add(preview); 

    count++; 

    if (count == 10) break; 
} 

return Json(new { Users = JsonConvert.SerializeObject(AllUsers, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore}) }, JsonRequestBehavior.AllowGet); 

ビューモデル:

public int Id { get; set; } 
public string UserName { get; set; } 
public string Photo { get; set; } 
public string Email { get; set; } 
public string Office { get; set; } 

角度コントローラ:

angular.module('app.module') 
.factory('Users', ['$http', function UsersFactory($http) { 
    return { 
     AllUsers: function() { 
      return $http({ method: 'GET', url: '/Controller/GetAllUsers' }); 
     } 
    } 
}]); 

angular.module('app.module') 
.controller('UserController', ['$scope', 'Users', function ($scope, Users) { 

    var vm = this; 


    Users.AllUsers().success(function (data) { 
     vm.users = JSON.stringify(data.Users); 
    }); 

}]); 

そして最後にビュー:

<table class="dataTable row-border hover" datatable="ng" dt-instance="vm.dtInstance" 
     dt-options="vm.dtOptions"> 
    <thead> 
     <tr> 
      <th class="secondary-text"> 
       <div class="table-header"> 
        <span class="column-title">Id</span> 
       </div> 
      </th> 
      <th class="secondary-text"> 
       <div class="table-header"> 
        <span class="column-title">Name</span> 
       </div> 
      </th> 
      <th class="secondary-text"> 
       <div class="table-header"> 
        <span class="column-title">Photo</span> 
       </div> 
      </th> 
      <th class="secondary-text"> 
       <div class="table-header"> 
        <span class="column-title">Email</span> 
       </div> 
      </th> 
      <th class="secondary-text"> 
       <div class="table-header"> 
        <span class="column-title">Office</span> 
       </div> 
      </th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr ng-repeat="user in vm.users"> 
      <td>{{user.Id}}</td> 
      <td>{{user.UserName}}</td> 
      <td><img class="product-image" ng-src="data:img/jpg;base64,{{user.Photo}}"></td> 
      <td>{{user.EmailAddress}}</td> 
      <td>{{user.Office}}</td> 
     </tr> 
    </tbody> 
</table> 

私は私が私のJSONから次のエラーを取得し、このコードを実行しようとするたび:

angular.js:13920 Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: user in vm.users, Duplicate key: string:\, Duplicate value: \

私はangleの提案された修正を使用しようとしました。これは$ indexによって追跡され、それが原因で私のページがフリーズします。

私はJSON文字列を返すときにFormatting.Indentedを取り出そうとしましたが、それは私に同じエラーを与えるだけでなく、ReferenceLoopHandling部分を取り出して同じエラーを取得することだけです。私はJSON.parse(データ)を実行しようとしました角度制御装置においては

と私は次のエラーを取得:私はlet users = data.Usersを行い、その後、それは私に思える番号85941を与えlet count = users.lengthをしようとすると

angular.js:13920 SyntaxError: Unexpected token o in JSON at position 1

を文字列のすべての文字を数えているように。

私はconsole.log(data)を実行すると、上に貼り付けたJSONを提供します(ユーザーの情報を安全に保つためにユーザー名、電子メール、場所を変更しました)。

ここで私は何が間違っているのか、それを修正する方法が全く分からないので、どんな助けも大歓迎です。

ありがとうございます!

+0

また、 'track by user.Id'を試してみました。 'エラー:[ngRepeat:dupes]リピータの重複は許可されていません。固有のキーを指定するには、 'track by'式を使用します。リピータ:ユーザーによってvm.usersのユーザーが追跡します.Id、重複するキー:未定義、重複する値: – DevShadow

+0

jQueryデータテーブルが問題を引き起こしている可能性はありますか? JSONはあなたが思っているものではないかもしれません。[このplunker](http://plnkr.co/edit/VGwvQuRM3SwSF84NSzap?p=preview)経由であなたのJSONを使用してreproはありません – ryanyuyu

+0

@ryanyuyu私はあなたの解決策を試しましたが、私はまだ同じエラーを取得しています – DevShadow

答えて

0

このエラーが最初に発生した理由が完全にはわかっていませんが、その修正はコントローラからJSONを戻すことではありませんでした。私はそうのような私のreturn文を変更:

return Json(JsonConvert.SerializeObject(AllUsers, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore}), JsonRequestBehavior.AllowGet); 

それから私がやった角度コントローラで:

vm.users = JSON.parse(data);

私に適切なJSON配列を与え、今では完璧に動作します。

関連する問題