我有一個引導程序導航標籤頁,我想在選擇標籤頁時動態顯示內容。每個選項卡必須顯示一個div,其中包含一些文本,這些文本是通過控制器的動作GetSection()
從ajax調用返回的。使用Ajax加載標籤頁
<div class="tabbable">
<ul class="nav nav-tabs" data-bind="foreach: sections">
<li data-bind="css: { active: isSelected }">
<a href="#" data-bind="click: $parent.selectedSection">
<span data-bind="text: name" />
</a>
</li>
</ul>
<div class="tab-content" data-bind="foreach: sections">
<div class="tab-pane" data-bind="css: { active: isSelected }">
<span data-bind="text: 'In section: ' + retValue" />
</div>
</div>
</div>
Javascript代碼:
var Section = function (name, selected) {
this.name = name;
this.retValue = "";
this.isSelected = ko.computed(function() {
return this === selected();
}, this);
}
var ViewModel = function() {
var self = this;
self.selectedSection = ko.observable();
self.sections = ko.observableArray([
new Section('Tab One', self.selectedSection),
new Section('Tab Two', self.selectedSection),
new Section('Tab Three', self.selectedSection)
]);
self.selectedSection(self.sections()[0]);
self.selectedSection.subscribe(function() {
$.ajax({
url: '@Url.Action("GetSection")',
data: { name: self.selectedSection().name },
type: 'GET',
success: function (data) {
self.selectedSection().retValue=data.text;
}
});
});
}
ko.applyBindings(new ViewModel());
的問題是,retValue
從阿賈克斯不會顯示。該控制器操作是這樣的:
public JsonResult GetSection(string name)
{
var ret = new { text = name + "abcd" };
return Json(ret, JsonRequestBehavior.AllowGet);
}
它是如何工作的數據綁定點擊,因爲$ parent.selectedSection不是一些功能? – albert
這本身就是一個真正的問題,但我試圖在我的答案的腳註中解決它。如果您需要進一步澄清,我建議您提出一個新的SO問題(可能首先搜索現有問題)。 – Jeroen