Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/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
如何通过AngularJS http.POST将long数组传递给ASP.NET核心控制器POST方法_Angularjs_Arrays_Post_Asp.net Core - Fatal编程技术网

如何通过AngularJS http.POST将long数组传递给ASP.NET核心控制器POST方法

如何通过AngularJS http.POST将long数组传递给ASP.NET核心控制器POST方法,angularjs,arrays,post,asp.net-core,Angularjs,Arrays,Post,Asp.net Core,我在AngularJS控制器中合并了两个阵列,并通过http.post发送: var selectedIds = angular.extend({}, $scope.selectedFirstKeys, $scope.selectedSecondKeys); $http.post("/api/tests/test/1", selectedIds ) .then(function (response) {

我在AngularJS控制器中合并了两个阵列,并通过http.post发送:

var selectedIds = angular.extend({}, $scope.selectedFirstKeys, $scope.selectedSecondKeys);
            $http.post("/api/tests/test/1", selectedIds )
                .then(function (response) {
                    ...                    
            });
使用Fiddler时,我看到数组是这样发送的:

{"0":22}
但当我这样定义POST方法时,它无法识别数组:

[HttpPost("test/{testId:long}")]
    public IActionResult Test(long testId, [FromBody] long[] selectedIds)
    {
        ...
    }

如何定义控制器方法以使其识别阵列?

考虑到要传递给后端逻辑的是阵列

此代码错误

var selectedIds = angular.extend({}, $scope.selectedFirstKeys, $scope.selectedSecondKeys);
  • 因为:它将
    $scope.selectedFirstKeys、
    $scope.selectedSecondKeys
    的内容复制到
    {}
    中(这是一个对象!),所以最终的结果也是一个对象:(
正确的方法(假设
$scope.selectedsecondkey
$scope.selectedsecondkey
是数组)是:

  • 因为:它设置了
    $scope.selectedFirstKeys
    $scope.selectedFirstKeys
    $scope.selectedSecondKeys
    ,这也将是一个数组!:)

您可能希望查看关于(和on)和(和on)的其他信息。摘录如下:


Array.prototype.concat

concat()方法返回由上的数组组成的新数组 它被称为与提供的数组和/或值联接 作为论据

语法

var new_array=old_array.concat(value1[,value2[,…[,valueN]])

参数

valueN
:要连接到新数组中的数组和/或值


角度。延伸

通过复制自己的可枚举项扩展目标对象
dst
src
对象到
dst
的属性

语法

angular.extend(dst,src)

参数

dst
:目标对象

src
:源对象


考虑到您想要传递给后端逻辑的是一个数组

此代码错误

var selectedIds = angular.extend({}, $scope.selectedFirstKeys, $scope.selectedSecondKeys);
  • 因为:它将
    $scope.selectedFirstKeys、
    $scope.selectedSecondKeys
    的内容复制到
    {}
    中(这是一个对象!),所以最终的结果也是一个对象:(
正确的方法(假设
$scope.selectedsecondkey
$scope.selectedsecondkey
是数组)是:

  • 因为:它设置了
    $scope.selectedFirstKeys
    $scope.selectedFirstKeys
    $scope.selectedSecondKeys
    ,这也将是一个数组!:)

您可能希望查看关于(和on)和(和on)的其他信息。摘录如下:


Array.prototype.concat

concat()方法返回由上的数组组成的新数组 它被称为与提供的数组和/或值联接 作为论据

语法

var new_array=old_array.concat(value1[,value2[,…[,valueN]])

参数

valueN
:要连接到新数组中的数组和/或值


角度。延伸

通过复制自己的可枚举项扩展目标对象
dst
src
对象到
dst
的属性

语法

angular.extend(dst,src)

参数

dst
:目标对象

src
:源对象


{“0”:22}
实际上不是一个数组,而是一个对象。数组在方括号中
[0,22,…]
。你确定你传递的是正确的吗?这就是angular.extend方法为我创建的。数据是正确的。带有一个项目(22)的数组是
$scope.selectedFirstKeys
$scope.selectedSecondKeys
数组?
{“0”:22}
实际上不是数组,而是对象。数组在方括号中
[0,22,…]
。你确定你传递的是正确的吗?这就是angular.extend方法为我创建的。数据是正确的。带有一个项目(22)的数组是
$scope.selectedFirstKeys
$scope.selectedSecondKeys
数组?