gpt4 book ai didi

javascript - knockout - 使用 View 模型数据传递子数组

转载 作者:行者123 更新时间:2023-12-03 08:45:19 25 4
gpt4 key购买 nike

我有一个如下所示的 viewModel:

var viewModel = new function () {

var self = this;
self.Id = ko.observable();
self.currentOrder = {

orderId: ko.observable(),
firstCropId: ko.observable(),
secondCropId: ko.observable(),

productDataList: ko.observableArray()
};


<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.0.0/knockout-min.js"></script>

我试图将 viewModel.currentOrder 传递给 MVC Controller ,如下所示:

self.SaveOrder = function () {
var url = '@Url.Action("SaveOrder", "Orders")';
$.mobile.loading('show');

var productInfo = {};

for (i = 0; i < 3; i++) {
productInfo = { Id: i, Rate: i + 11, Variable: i + 111 };
viewModel.currentOrder.productDataList.push(productInfo);
}


$.ajax({
type: 'POST',
url: url,
dataType: 'json',
data: viewModel.currentOrder,
traditional: true,
success: function (data, status) {

//success
},
error: function (xhr, ajaxOptions, thrownError) {
$.mobile.loading('hide');
}

});

}; //Save

Controller 方法如下所示:

public Function SaveOrder(vm As OrdersModels.EditViewModel) As JsonResult

Dim o As Sales.Order = GetCurrentOrder(vm.OrderId)

With o
.FirstCrop = vm.FirstCropId
.SecondCrop = vm.SecondCropId
.PreviousCrop = vm.PreviousFirstCropId

'code to save order
End function

OrdersModels.EditViewModel 看起来像这样:

Public Class EditViewModel
Public Property OrderId As Guid
Public Property FirstCropId As Integer
Public Property SecondCropId As Integer

Public Property ProductDataList As List(Of OrderProducts)
End Class

Public Class OrderProducts
Public Property Id As Integer
Public Property Rate As Decimal
Public Property Variable As Decimal
End Class

Controller 正在获取除 vm.ProductDataList 之外的所有数据。我尝试了各种方法,例如将 JSON.stringify(viewModel.currentOrder) 作为数据传递,添加 contentType: 'application/json; charset=utf-8' 没有运气。

如何将数据传递给 Controller ​​?

最佳答案

这应该有效

$.ajax({
type: 'POST',
url: url,
contentType:'application/json; charset=utf-8',
dataType: 'json',
data:ko.toJSON(self.currentOrder),
success: function (data, status) {
//success
},
error: function (xhr, ajaxOptions, thrownError) {
$.mobile.loading('hide');
}

});

关于javascript - knockout - 使用 View 模型数据传递子数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32917954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com