我已誠實地嘗試自己解決它。這是一個非常簡單的嘗試,但我無法使它工作! 現在,請在這裏問我的問題。Knockout.js數據未顯示在表
問題:可觀察數據不顯示在表中。
@using (Ajax.BeginForm("GetSubscriptionInfo", new AjaxOptions
{
HttpMethod = "POST",
OnSuccess = "ListsReceived"
}))
從服務器獲取數據,這裏是名單:
data: "[{"ListID":1,"ListName":"one list ","ListDescription":"one list description","OptIn":true},{"ListID":2,"ListName":"two list ","ListDescription":"two list description","OptIn":false},{"ListID":3,"ListName":"three list ","ListDescription":"three list description","OptIn":true},{"ListID":4,"ListName":"four list ","ListDescription":"four list description","OptIn":false},{"ListID":5,"ListName":"five list ","ListDescription":"five list description","OptIn":false},{"ListID":6,"ListName":"six list ","ListDescription":"six list description","OptIn":false}]"
在我的頁面的底部我有回調函數和數據結構的定義:
<script type="text/javascript">
function ListsReceived(data)
{
PageViewModel.subscriberLists = ko.mapping.fromJSON(data);
}
function PageViewModel() {
var self = this;
self.subscriberLists = ko.observableArray([]);
};
ko.applyBindings(new PageViewModel());
</script>
現在,我的表格:
<table class="table table-hover">
<caption class="text-left">Subscription lists and status:</caption>
<tr>
<th>Name</th>
<th>Description</th>
<th>Opt In</th>
</tr>
<tbody data-bind="foreach: subscriberLists()">
<tr>
<td data-bind="value: ListName"></td>
<td data-bind="value: ListDescription"></td>
<td data-bind="checked: OptIn"></td>
</tr>
</tbody>
</table>
我在這裏做錯了什麼?感謝幫助。
不,沒有幫助.. 我在這裏正確地做了映射: PageViewModel.subscriberLists = ko.mapping.fromJSON(data);? – HotFrost