这看起来很简单,但我是 Knockout JS 的新手。经过一系列教程后,我想出了这个:
// Create view model
var viewModel = function () {
var self = this;
self.master = ko.mapping.fromJS(@Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model)));
self.total_results = ko.observable(self.master.totalRecordCount());
self.pager = ko.pager(self.total_results);
self.pager().CurrentPage.subscribe(function () {
self.search();
})
self.search = function () {
$.ajax({
type: "GET",
url: "/api/get?data=1&start_index=" + self.pager().FirstItemIndex() + "&end_index=" + self.pager().LastItemIndex() + "",
}).done(function (pagedData) {
// Map model; create pager
ko.mapping.fromJS(pagedData, self.master);
//self.total_results(self.master.totalRecordCount());
}).error(function (ex) {
alert("Error");
});
}
}
$(function () {
// Apply
ko.applyBindings(viewModel);
});
当我单击分页按钮时,搜索方法将运行并获取新数据。当我更新 ViewModel 时...什么也没有发生。有什么想法吗?
最佳答案
您想要更新self.master
的内容,而不是覆盖它。因此,将 done
回调中的该行更改为:
// Map model; create pager
ko.mapping.fromJS(pagedData, self.master);
关于javascript - ASP.net MVC/Knockout JS 映射 - 无法更新 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27018194/