我想从一个基本上是 AJAX 帖子的动态模型创建一个可观察数组,以获取 JSON 信息。然后我想将该数组添加到表中。
这是我创建 Viewmodel 的 Javascript,以及要添加到数组的内容:
var ProductViewmodel;
function bindProductModel(Products) {
var self = this;
self.items = ko.mapping.fromJS([]);
ProductViewmodel = ko.mapping.fromJS(Products, self.items);
console.log(ProductViewmodel);
ko.applyBindings(ProductViewmodel);
}
function JSONProducts() {
$.ajax({
url: "WebForm1.aspx/AvailibleProducts",
// Current Page, Method
data: '{Warehouse: 1}',
// parameter map as JSON
type: "POST",
// data has to be POSTed
contentType: "application/json; charset=utf-8",
// posting JSON content
dataType: "JSON",
// type of data is JSON (must be upper case!)
timeout: 10000,
// AJAX timeout
success: function (result) {
bindProductModel(result);
},
error: function (xhr, status) {
alert(status + " - " + xhr.responseText);
}
});
}
JSON的获取工作完美:
{
"d": [
{
"__type": "Warehouse.Tracntrace.Members_Only.StockMovement.ProductStagingMethod",
"ProductSKUID": 2,
"ProductSKUName": "Decoder 1131",
"WarehouseID": 1,
"WarehouseName": "SoftwareDevelopmentTest",
"SystemAreaName": "Staging",
"Qty": 5
}
]
}
这是我尝试将它数据绑定到我的表的地方:
<div id="TableContainer" class="gridview">
<table border="1" cellpadding="0" cellspacing="0">
<tbody data-bind="foreach: ProductViewmodel">
<tr>
<td data-bind="text: ProductSKUID"></td>
<td data-bind="text: ProductSKUName"></td>
<td data-bind="text: WarehouseID"></td>
<td data-bind="text: WarehouseName"></td>
<td data-bind="text: SystemAreaName"></td>
<td data-bind="text: QTY"></td>
</tr>
</tbody>
</table>
</div>
似乎它不想将它添加到我的数组中。任何建议将不胜感激。
问候雅克