我有一个 MVC 4 单页应用程序,我可以在其中看到数据视图模型的淘汰代码。我在视图中做了一个 foreach ,每次通过循环时都可以看到一个点,但数据没有显示。看起来可观察数组有 50 个元素,但每个元素都没有数据。然而,当我单步执行 getJson 调用时,我可以看到 json 格式的数据。怎么了?
这是我的 caapproval.html 视图代码-
<section>
<h2 class="page-title" data-bind="text: title"></h2>
<br /> <br /> <br />
<ul data-bind="foreach: allCertificates">
<li>
<span data-bind="text: UserName"></span>
<span data-bind="text: StatusCode"></span>
</li>
</ul>
</section>
我的 caapproval.js 视图模型代码-
define(['services/logger'], function (logger) {
var Certificate = function (ClientID, RequestDate, UserName, StatusDescription, StatusCode, StatusDesc, CEOUserName ) {
var self = this;
self.ClientID = ko.observable(ClientID);
self.RequestDate = ko.observable(RequestDate);
self.UserName = ko.observable(UserName);
self.StatusDescription = ko.observable(StatusDescription);
self.StatusCode = ko.observable(StatusCode);
self.StatusDesc = ko.observable(StatusDesc);
self.CEOUserName = ko.observable(CEOUserName);
};
var allCertificates = ko.observableArray();
var vm = {
allCertificates: allCertificates,
activate: activate,
title: 'Certificates'
};
return vm;
function getallCertificates() {
$.getJSON('/api/caapproval', function (data) {
allCertificates([]);
var temp = allCertificates();
data.forEach(function (p) {
var certificate = new Certificate(p.ClientID, p.RequestDate, p.UserName, p.StatusDescription, p.StatusCode, p.StatusDesc, p.CEOUserName);
temp.push(certificate);
});
allCertificates.valueHasMutated();
return allCertificates();
});
}
//#region Internal Methods
function activate() {
logger.log('CA Approval View Activated!!', null, 'CA Approval', true);
return getallCertificates();
}
//#endregion
});