我有一些問題發佈到我的控制器從Angular。如果我單獨發佈變量,一切都很好,但是當我將我的變量組合成一個對象來響應我的視圖模型時,它只識別ID而不是我的PlayerViewModel的列表。模型從AngularJS到ASP.NET MVC5
這裏是我的viewModels:
public class PlayerViewModel
{
public int ID { get; set; }
public string Name { get; set; }
}
public class TeamViewModel
{
public int ID { get; set; }
public List<PlayerViewModel> Players;
}
這是我的MVC控制器:
[HttpPost]
public JsonResult IndexPost(int ID, List<PlayerViewModel> ListPlayers, TeamViewModel Team)
{
return Json(true, JsonRequestBehavior.AllowGet);
}
這是我的角碼:
$scope.players = [];
$scope.playerA = { ID: 5, Name: "Player1" };
$scope.playerB = { ID: 6, Name: "Player2" };
$scope.playerC = { ID: 8, Name: "Player3" };
$scope.players.push($scope.playerA);
$scope.players.push($scope.playerB);
$scope.players.push($scope.playerC);
$scope.ID = 7;
$scope.team = { ID: $scope.ID, Players: $scope.players };
$scope.btnClick = function() {
$http({
traditional: true,
url: "IndexPost",
method: "POST",
data: { 'ID': $scope.ID, 'ListPlayers': $scope.players, 'Team': $scope.team },
dataType: FormData
}).success(function (data) {
$scope.finish = data;
}).error(function (data) {
$scope.status = status;
});
}
這是我在控制器響應:
正如你看到的,我TeamViewModel的ID是正確的,但PlayerViewModels的名單不會填寫有沒有人有一個想法是什麼我。做錯了嗎?
謝謝!
我也有同樣的問題。你解決了這個問題? – 2016-11-12 07:43:29
是的,首先嚐試從後端獲取一個空模型,然後操縱該模型,而不是創建一個新的JS變量。所以,從一個HTTP Get開始,操作這個模型,然後做一個HTTP Post。這對我工作.. – brammekuhh 2016-11-14 12:12:49