Controller从angularjs服务帖子接收Null参数

问题描述 投票:0回答:1

我在使用angularjs向控制器发布数据时遇到问题。

调试时数据看起来很好,直到它到达我的控制器,然后传入的参数总是为空。

public class NewCompArgs {
            public string Name { get; set; }
            public string Description { get; set; }

        }

        //Tested with [HttpPost] here, and it didn't work
        // Tested using [FromBody]NewCompArgs args, and splitting 
        // out into Name and Decription strings with [FromBody]
        public ActionResult AddNewComponent(NewCompArgs args) 
        {
            Component NewComp = new Component
            {
                Name = args.Name,
                Description = args.Description
            };
            _context.Add(NewComp);
            _context.SaveChanges();
            return Json(NewComp);
        }

NewCompArgs args每次都为null。

这是我用来击中控制器的服务:

app.factory('Components', function ($http, $q) {
    var service = {};

    service.AddNewComponent = function (args) {
        var deferred = $q.defer();
        $http.post('/Components/AddNewComponent', { args: args }).then(function (response) {
            deferred.resolve(response.data);
        });
        return deferred.promise;
    };
    return service;
});

这是角度控制器功能:

$scope.newComponent = function () {
        Components.AddNewComponent($scope.compArgs).then(function (response) {
        });
    }

只是我在调试过程中得到的数据的一个例子

角度控制器:ng controller

角度服务:ng service

MVC控制器:MVC Controller

任何意见是极大的赞赏!

c# .net angularjs model-view-controller
1个回答
1
投票

我不确定这是否有用,但是在Angular 2/5中,当你想在控制器中触发一个方法时你可以使用method()。subscribe();也许这会对你有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.