简体   繁体   English

我无法访问asp.net mvc控制器以使用$ resource of angular向我返回JsonResult

[英]I am unable to access an asp.net mvc controller to return me a JsonResult using $resource of angular

What am I missing ? 我想念什么? I am new to Angularjs. 我是Angularjs的新手。 Trying angularjs with asp.net mvc. 使用asp.net mvc尝试angularjs。 I am unable to access an asp.net mvc controller to return me a JsonResult using $resource of angular. 我无法访问asp.net mvc控制器以使用angular的$ resource返回JsonResult。 However, I get success otherwise using $.getJson of javascript but not using angularjs. 但是,否则我会使用JavaScript的$ .getJson而不是angularjs获得成功。 What am I missing ? 我想念什么? please guide. 请指导。 Thank you for replying any. 感谢您的答复。

Following is my Service 以下是我的服务

EbsMvcApp.factory('classListService', function ($resource, $q)
{
    var resource = $resource
                    (
                      '/Home/ClassList' 
                      , {}
                     //{ method: 'Get', q: '*' }, // Query parameters
                       , { 'query': { method: 'GET' , isArray:false  } }
                    );

    function get($q)
    {
        console.log('Service: classListServic > Started');

        var Defered = $q.defer();

        resource.get
            (
                 function (dataCb)
                 {
                     console.log('success in http service call');
                     Defered.resolve(dataCb);
                 }
                , function (dataCb)
                {
                    console.log('error in http service')
                    Defered.reject(dataCb);
                }
            );
        return Defered.promise; // if missed, would throw an error on func: then.
    };
    return { get: get };
});

angular Controller: 角度控制器:

var EbsMvcApp = angular.module('myApp', ['ngResource']);

//'classListService',
EbsMvcApp.controller
    (
        'myAppController',
        ['$scope','classListService','$q' , function ($scope, classListService, $q)
            {
                    console.log('controller myAppController started');

                    var classList = classListService.get($q);
                   classList =  classList.then( 

                        function ()
                        {
                            (
                            function (response)
                            {
                                console.log('class list function response requested');
                                return response.data;
                            }
                        );
                       }
                    );

                   console.log(classList.ClassName);
                   console.log(classList);

                    console.log('end part of ctrl');
                    $scope.classList = classList;
                    $scope.SelectedClassID = 0;
                    $scope.message = ' message from Controller ';

            }
        ]
    );

Asp.net MVC Controller Asp.net MVC控制器

namespace EBS_MVC.Controllers
    {
        public class HomeController : BaseController
        {
            ApplicationDbContext db = new ApplicationDbContext();

                public JsonResult ClassList()
            {
                var List = new SelectList(db.tblClass, "ID", "ClassName");

                return Json(List, JsonRequestBehavior.AllowGet);
            }
       }
    } 

Brower's response (F12): 浏览器的响应(F12):

ControllerTry1.js:11 controller myAppController started serviceGetClassList.js:16 Service: classListServic > Started ControllerTry1.js:28 undefined ControllerTry1.js:29 c ControllerTry1.js:31 end part of ctrl angular.js:12520 Error: [$resource:badcfg] [Browers response: screen shot][1] ControllerTry1.js:11控制器myAppController已启动serviceGetClassList.js:16服务:classListServic>已启动ControllerTry1.js:28未定义ControllerTry1.js:29 c ControllerTry1.js:31 ctrl angular.js的结尾部分:12520错误:[$资源: badcfg] [浏览器响应:屏幕截图] [1]

Oky, finally, I got a solution using the $http service. 好的,最后,我得到了使用$ http服务的解决方案。 from here 从这里

http://www.infragistics.com/community/blogs/dhananjay_kumar/archive/2015/05/13/how-to-use-angularjs-in-asp-net-mvc-and-entity-framework-4.aspx http://www.infragistics.com/community/blogs/dhananjay_kumar/archive/2015/05/13/how-to-use-angularjs-in-asp-net-mvc-and-entity-framework-4.aspx

in csHtml file, a reference to the service.js and Controler.js is required. 在csHtml文件中,需要引用service.js和Controler.js。 I am not sure if I have added it earlier or later now. 我不确定是否早晚添加了它。 but its required. 但它是必需的。

ng-Controller: ng-Controller:

    EbsMvcApp.controller('ClassListController', function ($scope, ClassListService2) {    

            console.log('ClassListController Started');

            GetClassList();

            function GetClassList()
            {
                ClassListService2.GetJson()
                        .success(function (dataCallBack) {
                            $scope.classList = dataCallBack;
                            console.log($scope.classList);
                        })
                        .error(function (error) {
                            $scope.status = 'Unable to load data: ' + error.message;
                            console.log($scope.status);
                        });
            }
        });

ng-Service: ng-Service:

    EbsMvcApp.factory('ClassListService2', ['$http', function ($http) {


    console.log('ClassListService2 Started');

    var list = {};
    list.GetJson = function () {
        return $http.get('/Home/ClassList');
    };
    return list;

}]);

csHtml View: csHtml查看:

    <div class="text-info" ng-controller="ClassListController">
<h3> Text from Controller: </h3>

@*table*@
<table class="table table-striped table-bordered">
    <thead>
        <tr><th>DisplayName</th><th>Value</th>
    </thead>
    <tbody>
        <tr ng-hide="classList.length">
            <td colspan="3" class="text-center">No Data</td>
        </tr>
        <tr ng-repeat="item in classList">
            <td>{{item.Text}}</td>
            <td>{{item.Value}}</td>

        </tr>
    </tbody>
</table>

Sorry for the delay, I just wrote up some code to quickly test the ngResource module as I haven't used it yet. 抱歉,延迟,我刚刚写了一些代码来快速测试ngResource模块,因为我还没有使用它。

I've got the code working to do what you want using the ngResource module. 我已经使用ngResource模块来完成您想要的代码。 I think part of the problem was that you was configuring the query method but calling the get method so your configurations was not applied. 我认为部分问题是您正在配置查询方法,但调用了get方法,因此未应用您的配置。

Here is the service class that I wrote to test against a controller the same as yours. 这是我编写的用于针对与您的控制器相同的控制器进行测试的服务类。

(function () {
    'use strict';

    angular
        .module('myApp')
        .service('classService', ClassService);

    ClassService.$inject = ['$resource', '$q'];

    function ClassService($resource, $q) {

        var resource = $resource
        (
            '/Home/ClassList',
            {},
            {
                'get': { method: 'GET', isArray: true },
                'query': { method: 'GET', isArray: true }
            }
        );

        var service = {
            get: get
        };

        return service;

        ////////////

        function get() {
            var Defered = $q.defer();

            resource.get(function (dataCb) {
                console.log('success in http service call');
                Defered.resolve(dataCb);
            }, function (dataCb) {
                console.log('error in http service')
                Defered.reject(dataCb);
            });

            return Defered.promise;
        };
    };
})();

The controller looks like this 控制器看起来像这样

(function () {
    'use strict';

    angular
        .module('myApp')
        .controller('classController', ClassController);

    ClassController.$inject = ['$scope', 'classService'];

    function ClassController($scope, classService) {

        var vm = this;
        vm.data = null;

        activate();

        /////////////

        function activate() {

            var classList = classService.get().then(function (response) {
                console.log('class list function response requested');
                vm.data = response;
                console.log(vm.data);
            });

            console.log('end part of ctrl');
            $scope.SelectedClassID = 0;
            $scope.message = ' message from Controller ';
        };

    };
})();

I've included some of your original code just so you can see how it would fit in. 我已经包含了一些原始代码,只是您可以看到它的用法。

Glad to see you have got it working though! 很高兴看到您已经开始使用它了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM