【发布时间】:2016-05-15 14:30:13
【问题描述】:
我想在 MVC4 中实现 AngularJS。如何在 mvc4 中以 angular 格式返回 JSON。
这是我的代码:
控制器:
[HttpGet]
public ActionResult sample1()
{
return Json(db.Account_Info.ToList(), JsonRequestBehavior.AllowGet);
}
Account.js
App.controller('Account', ['$scope', '$http', '$timeout', function ($scope, $http, $timeout)
{
$scope.bag = [];
$scope.alldata = function ()
{
//
$http.get('/Accounts/sample1').then(function ($response)
{
$scope.bag = $response;
});
}
//execute on the page load
$timeout(function ()
{
$scope.alldata();
});
}]);
App.js
var App = angular.module('MyApp', []);
查看:
<script src="~/Scripts/angular/angular.min.js"></script>
<script src="~/Scripts/angular/App.js"></script>
<script src="~/Scripts/angular/Account.js"></script>
<h2>sample</h2>
<div ng-app="MyApp" >
<div ng-controller="Account">
<div ng-repeat="acc in bag">
<p>Username: {{acc.username}}</p>
</div>
</div>
</div>
现在,我遇到了这个错误:
A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.Account_Info_D9C702CA15FC076225862589F60CD5E8B8EA615EF98D78A1FEB764E267D88F97'.
从控制器返回数据:
需要建议和帮助。提前致谢。
【问题讨论】:
-
您的错误与角度无关。你有一些东西和对象/类在循环引用中,比如 A -> B,B -> C,C-> A。
-
我将如何解决这个错误?我应该在控制器中做什么?
-
是的,我已经尝试了所有这些但仍然没有显示数据
-
我返回的是数据列表,而不仅仅是一行。
标签: javascript angularjs json asp.net-mvc-4 json.net