无法显示从MVC控制器传递到角度工厂的数据

时间:2015-10-20 08:39:09

标签: angularjs asp.net-mvc angularjs-controller

我从MVC控制器JSON返回List<User>,但我无法在我的视图中加载用户详细信息。

这是我的代码:

UserMasterController.cs

public class UserMasterController : Controller
{
    private readonly ILoginService _LoginService;
    private readonly IMenuService _MenuService;
    private readonly IUserMasterRepository _UserMasterRepository;

    public UserMasterController(ILoginService LoginService, IMenuService MenuService, IUserMasterRepository UserMasterRepository)
    {

        _MenuService = MenuService;
        _LoginService = LoginService;
        _UserMasterRepository = UserMasterRepository;
    }

    public JsonResult GetUserMasters()
    {
        MainViewModel MainModel = new MainViewModel();
        MainModel.UserMaster = _UserMasterRepository.Display();
        var data = MainModel.UserMaster; 
        return  Json(  data,JsonRequestBehavior.AllowGet );
    }

    [HttpGet]
    [CustomAuthorize(AccessLevel = "Administrator")]
    public ViewResult Index()
    {
        return View();
    } 
}

GetUserMasters.js

var TimesheetManagementApp = angular.module('TimesheetManagementApp')
TimesheetManagementApp.controller('GetUserMasters', function ($scope,UserMasterService) {
    GetUserMasters();
    function GetUserMasters() {
        UserMasterService.GetUserMasters()
        .success(function (users) {
            $scope.UserMaster = users;

        })
        .error(function (error) {
            $scope.status = '' + error.message+'';
            alert($scope.status)
        });
    }
});

TimesheetManagementApp.factory('UserMasterService', ['$http', function ($http) {
    var UserMasterService = {};
    UserMasterService.GetUserMasters = function () {
        return $http.get('/UserMaster/GetUserMasters');
    };
    return UserMasterService;
}]);

Index.cshtml

<body ng-app="TimesheetManagementApp">
    <div class="col-md-9">
        <div ng-controller="GetUserMasters">
            <table class="tableData" w border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <th>
                        First Name
                    </th>
                    <th>
                        Middle Name
                    </th>
                    <th>
                        Last Name
                    </th>
                    <th>
                        Email Id
                    </th>
                    <th>
                        Role
                    </th>
                </tr>

                <tr ng-repeat="user in UserMaster" ng-class-odd="'odd'" ng-class-even="'even'">
                    <td>{{user.FirstName}}</td>
                    <td>{{user.MiddleName}}</td>
                    <td>{{user.LastName}}</td>
                    <td>{{user.EmailId}}</td>
                </tr>
            </table>
        </div>
    </div>

<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular-route.min.js"></script>
@Scripts.Render("~/bundles/TimesheetManagementApp")
<script src="~/Scripts/TimesheetApp/TimesheetManagementApp.js"></script>

<script src="~/Scripts/TimesheetApp/Controller/GetUserMasters.js">
</script>

</body>
</html>

0 个答案:

没有答案