我是Knockout的新手,当用户在网站中导航时,我想在我的MVC项目中加载局部视图。 部分视图加载工作正常,但我需要将项添加到ko.observable数组中,该数组在列表中呈现。当我尝试推送一个项目时,我收到消息“nom未定义。 这是视图模型:
var service = function (nom) {
this.nom = ko.observable(nom)
};
var services = ko.observableArray([new service('Hello')]);
var plainteModel = {
DocumentePar: ko.observable('Michel Bernier'),
Date: ko.observable('12-12-2016'),
nextStep: function () {
$.ajax({
url: '/Plainte/GetSection1',
type: 'GET',
async: false,
success: function (results) {
$('#sectionRendering').html(results);
},
error: function (error) {
}
})
services.push([new service('Test')]);
ko.applyBindings(plainteModel, $('#sectionRendering')[0]);
},
prevStep: function () {
}
};
ko.applyBindings(plainteModel);
主要观点:
@{
ViewBag.Title = "Plainte";
}
@Styles.Render("~/Content/Plainte")
@Scripts.Render("~/bundles/modernizr")
<div class="content">
<div class="sidebar1">
<div id="sidebarContainer">
<ul id="sections">
<li><button>Section 1</button></li>
<li><button>Section 2</button></li>
<li><button>Section 3</button></li>
<li><button>Section 4</button></li>
<li><button>Section 5</button></li>
<li><button>Section 6</button></li>
<li><button>Section 7</button></li>
<li><button>Section 8</button></li>
<li><button>Section 9</button></li>
</ul>
</div>
</div>
<div class="controls">
<ul class="breadcrumb">
<li><a href="#">Acceuil</a></li>
<li><a href="#">Plainte</a></li>
<li class="active">Plainte No.9999</li>
</ul>
<div id="controlButtons"><button>Annuler</button> <button>Enregistrer</button></div>
</div>
<div id="sectionRendering">
</div>
</div>
<div class="navigation">
<div id="navigationControls">
<button id="navPrev" data-bind="click: prevStep">Précédent</button><button id="navNext" data-bind="click: nextStep">Suivant</button>
</div>
</div>
<footer id="plainteFooter">
<div class="tabsExpanded" onclick="collapseTabs()" id="closeTabsBtn"></div>
<div id="plainteTabs">
<ul>
<li><a href="#tabs-1">Pièces jointes</a></li>
<li><a href="#tabs-2">Journal</a></li>
</ul>
<div id="tabs-1">
<p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollicitudin mi sit amet mauris. Nam elementum quam ullamcorper ante. Etiam aliquet massa et lorem. Mauris dapibus lacus auctor risus. Aenean tempor ullamcorper leo. Vivamus sed magna quis ligula eleifend adipiscing. Duis orci. Aliquam sodales tortor vitae ipsum. Aliquam nulla. Duis aliquam molestie erat. Ut et mauris vel pede varius sollicitudin. Sed ut dolor nec orci tincidunt interdum. Phasellus ipsum. Nunc tristique tempus lectus.</p>
</div>
<div id="tabs-2">
<p>Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc. Duis scelerisque molestie turpis. Sed fringilla, massa eget luctus malesuada, metus eros molestie lectus, ut tempus eros massa ut dolor. Aenean aliquet fringilla sem. Suspendisse sed ligula in ligula suscipit aliquam. Praesent in eros vestibulum mi adipiscing adipiscing. Morbi facilisis. Curabitur ornare consequat nunc. Aenean vel metus. Ut posuere viverra nulla. Aliquam erat volutpat. Pellentesque convallis. Maecenas feugiat, tellus pellentesque pretium posuere, felis lorem euismod felis, eu ornare leo nisi vel felis. Mauris consectetur tortor et purus.</p>
</div>
</div>
</footer>
@section Scripts {
@Scripts.Render("~/bundles/Plainte")
}
部分视图:
<ul data-bind="foreach: services">
<li>
<div>
<span data-bind="text: nom"> </span>
</div>
</li>
</ul>
<h2>Plainte</h2>
<div>Plainte documenté par: <input data-bind="textInput: DocumentePar" /></div>
<div>Date de l'événement: <input data-bind="text: Date" /></div>
<button data-bind="click: savePlainte">Sauvegarder</button>
<button data-bind="click: getPlainteInfo.bind($data,1)">Test getInfo</button>
如果我使用nextstep函数中的push删除该行,一切正常,我得到主视图,部分视图和KO绑定一切正常,但如果我尝试推送新服务,我收到消息“ nom未定义“。
我正在敲打我的头5个小时!!
感谢您的帮助。