[英]How to pass array of long to ASP.NET Core Controller POST method via AngularJS http.post
我在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)
{
...
}
如何定義Controller方法以便識別數組?
考慮到你想要傳遞給后端邏輯的是一個數組。
這段代碼錯了
var selectedIds = angular.extend({}, $scope.selectedFirstKeys, $scope.selectedSecondKeys);
$scope.selectedFirstKeys,
和$scope.selectedSecondKeys
的內容復制到{}
(這是一個對象!),所以最終的結果也是一個對象! :( 正確的方法(假設$scope.selectedSecondKeys
和$scope.selectedSecondKeys
是數組)是:
$scope.selectedFirstKeys = $scope.selectedFirstKeys.concat($scope.selectedSecondKeys);
$scope.selectedFirstKeys
,結果是cancatenating $scope.selectedFirstKeys
和$scope.selectedSecondKeys
,這也是一個數組! :) 您可能希望查看有關Array.prototype.concat (和SO )和angular.extend (以及SO )的其他信息。 提取物如下所示:
Array.prototype.concat
concat()方法返回一個新數組,該數組由調用它的數組組成,並與作為參數提供的數組和/或值連接。
句法
var new_array = old_array.concat(value1[, value2[, ...[, valueN]]])
參數
valueN
:要連接成新數組的數組和/或值。
angular.extend
通過將自己的可枚舉屬性從
src
對象復制到dst
來擴展目標對象dst
。句法
angular.extend(dst, src);
參數
dst
:目標對象
src
:源對象
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.