I wanted to implement AngularJS in MVC4. How will I return JSON in the format of angular in mvc4.
Here are my codes:
Controller:
[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', []);
View:
<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>
Now, I've got this error:
A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.Account_Info_D9C702CA15FC076225862589F60CD5E8B8EA615EF98D78A1FEB764E267D88F97'.
Return data from the controller:
Need advice and help. Thanks in advance.