【发布时间】:2015-01-07 15:53:10
【问题描述】:
我正在熟悉 angular.js,使用 $Http.Get 从文件中返回 Json 数据似乎很容易。
在一个例子中,我会像这样得到我的 json 数据
var artistControllers = angular.module('artistControllers', []);
artistControllers.controller('ListController', function ($scope, $http) {
$http.get('json/jsonAngular.txt').success(function (data) {
$scope.artists = data;
});
});
例如,我如何获得 JsonResult 并将其分配给我的 $scope.artists。
[HttpPost]
public JsonResult GetArtists()
{
ViewModel model = new ViewModel();
model.GetArtists();
return Json(new
{
Artists = model.Artists
});
}
以我的班级为例
public class Artist
{
public string Initial { get; set; }
public string Surname { get; set; }
}
是否有一个可行的示例说明我可以返回 JsonResult 并将其呈现在我的 html 中。
【问题讨论】:
-
你也没有。 JSON 数据 1:1 javascript 对象。因此,您的 json 数据已经绑定为 scope.artists 中的对象,并且可以在此范围内访问,如答案 1。
标签: javascript asp.net-mvc json angularjs