Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 控制器从angularjs服务post接收空参数_C#_.net_Angularjs_Model View Controller - Fatal编程技术网

C# 控制器从angularjs服务post接收空参数

C# 控制器从angularjs服务post接收空参数,c#,.net,angularjs,model-view-controller,C#,.net,Angularjs,Model View Controller,我在使用angularjs向控制器发布数据时遇到问题 数据在调试时看起来很好,直到它到达我的控制器,然后传入的参数总是空的 public class NewCompArgs { public string Name { get; set; } public string Description { get; set; } } //Tested with [HttpPost] here, and it didn't

我在使用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) {
        });
    }
这只是我在调试期间获得的数据的一个示例

角度控制器:

角度服务:

MVC控制器:


非常感谢您的建议

我不确定这是否有帮助,但在角度2/5的情况下,当你想在控制器中触发一个方法时,你可以使用method.subscribe;也许这对你有帮助

他正在使用angularjs。我知道,这就是我不确定它是否有帮助的原因,但他应该尝试。您正在传递{args:args},但服务器需要一个包含名称和描述的对象,而不是args。您是否尝试使用PostMan API客户端点击此方法?它在那里工作正常吗?可能是@osm0sis的副本,您正在返回ActionResult-它是错误的。我认为您正在扩展控制器而不是ApiController。