I am new to AngularJS and need to use AngularJs to render my MVC controller Json output. Below is my MVC Controller that output Json:
[HttpGet] public JsonResult GetAllData() { int Count = 50; return Json(Workflow.Teacher.GetTeachers(Count), JsonRequestBehavior.AllowGet); }
My Angular Controller that calls the GetAllData Action method:
angular.module('myFormApp', []) .controller('HomeController', function ($scope, $http, $location, $window) { $scope.teacherModel = {}; $scope.message = ''; $scope.result = "color-default"; $scope.isViewLoading = false; $scope.ListTeachers = null; getallData(); //******=========Get All Teachers=========****** function getallData() { $http({ method: 'GET', url: '/Home/GetAllData' }).then(function successCallback(response) { // this callback will be called asynchronously // when the response is available $scope.ListTeachers = response; console.log($scope.ListTeachers); }, function errorCallback(response) { // called asynchronously if an error occurs // or server returns response with an error status. $scope.errors = []; $scope.message = 'Unexpected Error while saving data!!'; console.log($scope.message); }); }; }) .config(function ($locationProvider) { $locationProvider.html5Mode(true); });
Further my MVC layout markup is bellow:
@{ Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Teachers List</h2> <div id="content" ng-controller="HomeController"> <span ng-show="isViewLoading" class="viewLoading"> <img src="~/Content/images/ng-loader.gif" /> loading... </span> <div ng-class="result">{{message}}</div> <table class="table table-striped"> <tr ng-repeat="teacherModel in ListTeachers"> <td>{{teacherModel.TeacherNo}}</td> <td>{{teacherModel.TeaFName}}</td> <td>{{teacherModel.TeaSName}}</td> </tr> </table> </div> @section JavaScript{ <script src="~/Scripts/angular.js"></script> <script src="~/ScriptsNg/HomeController.js"></script> }
further to above my main layout's body tag also have ng-app
<body ng-app="myFormApp" >
I am using MVC version 5 with AngularJs v1.6.4. On debugging I can confirm that it does call getallData() actionMethod and does return rows in Json. I am not getting any error but its not rendering the model values either.
Any suggestions would be appreciated.