[asp.net-mvc]使用 angularjs 对 Asp.NET MVC Json 的数据访问

发布时间: 2017/3/25 0:50:26
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

你好我有问题的家伙。其实我不知道热访问 json 与 angularjs 的一些性能数据。我只学习 angularjs。

<div ng-controller="KurumsalYonetimCtrl">
    <p>{{model | json}}</p>
    <div ng-repeat="item in model">
        <p>{{item.Id}}</p>
        <p>{{item.Baslik}}</p>
        <p>{{item.Aciklama}}</p>
        <p>{{item.ResimYolu}}</p> /*This is PicturePath but it doesnt show*/
    </div>

</div>

AngularJS 控制器代码

    var app = angular.module('KurumsalYonetimController', []);
    app.controller('KurumsalYonetimCtrl', ['$scope', '$http', function ($scope, $http) {
        $scope.model = {};
        $http.get('/KUN/KurumsalYonetim/JsonListele').success(function (data) {
            $scope.model = data;
        });
    }]);

JSON 数据

[
      {
        "Pictures": [
          {
            "history": null,
            "landTransportation": null,
            "partialManagment": null,
            "projectTransportation": null,
            "userDetail": null,
            "Baslik": "Hakkimizda Resim",
            "Kategori": "Genel",
            "Aciklama": null,
            "ResimYolu": "1b066060-d230-45d7-a7ed-fc87bf3bd7d1_profile_avatar.jpg",
            "aboutUsId": 5,
            "historyId": null,
            "landTransportationId": null,
            "projectTransportationId": null,
            "partialManagmentId": null,
            "userDetailId": null,
            "Id": 5,
            "isDeleted": false,
            "CreatedDate": "2015-07-09T02:02:44.473",
            "UpdatedDate": "2015-07-09T02:02:44.473",
            "hasPicture": false
          }
        ],
        "userDetail": null,
        "Baslik": "deneme baslik",
        "Aciklama": "deneme aciklama",
        "userDetailId": null,
        "Id": 5,
        "isDeleted": false,
        "CreatedDate": "2015-07-09T02:02:44.16",
        "UpdatedDate": "2015-07-09T02:02:44.553",
        "hasPicture": true
      }
    ]

我不能访问图片在此 json 的结果,但我可以访问这些数据。我如何可以也访问图片

  "userDetail": null,
    "Baslik": "deneme baslik",
    "Aciklama": "deneme aciklama",
    "userDetailId": null,
    "Id": 5,
    "isDeleted": false,
    "CreatedDate": "2015-07-09T02:02:44.16",
    "UpdatedDate": "2015-07-09T02:02:44.553",
    "hasPicture": true
  }

解决方法 1:

你的 JSON 显得不对劲。您可以访问的下半部分的原因是您要遍历对象的数组。

 {
        "Pictures": [
          {
            "history": null,
            "landTransportation": null,
            "partialManagment": null,
            "projectTransportation": null,
            "userDetail": null,
            "Baslik": "Hakkimizda Resim",
            "Kategori": "Genel",
            "Aciklama": null,
            "ResimYolu": "1b066060-d230-45d7-a7ed-fc87bf3bd7d1_profile_avatar.jpg",
            "aboutUsId": 5,
            "historyId": null,
            "landTransportationId": null,
            "projectTransportationId": null,
            "partialManagmentId": null,
            "userDetailId": null,
            "Id": 5,
            "isDeleted": false,
            "CreatedDate": "2015-07-09T02:02:44.473",
            "UpdatedDate": "2015-07-09T02:02:44.473",
            "hasPicture": false
          }
        ],
        "userDetail": null,
        "Baslik": "deneme baslik",
        "Aciklama": "deneme aciklama",
        "userDetailId": null,
        "Id": 5,
        "isDeleted": false,
        "CreatedDate": "2015-07-09T02:02:44.16",
        "UpdatedDate": "2015-07-09T02:02:44.553",
        "hasPicture": true
      }

那就是所有一个对象,该对象是你 item 在你 ng-repeat 。所以,当你循环,它可以抓取 Id, Aciklama ,等等。你不获取 Id 为图片的原因是因为它是一个嵌套的对象数组。你要去另一个级别得到 Id, Baslik ,里面的图片数组等。

循环的图片,这样做︰ $scope.Pictures = model[0].Pictures; ,然后item in Pictures

你的 JSON 看起来对我格式不正确。

编辑

后在评论中的一些反馈,JSON 看起来像是它应该的方式。你只需要更改详细以上才能进去你吴重复的图片数据。

官方微信
官方QQ群
31647020