我有一个viewModel answerGroup对象的数组。当其中一个answerGroup对象的feedback属性更新时,我想通过将ajax传递给我的ASP.Net MVC应用程序来将更新的对象保存到我的数据库中。事件绑定在knockout.js
而不是有一个典型的保存按钮或链接,我希望对象的属性已更新时传递给Ajax调用。我想我可以通过绑定到textarea元素的change事件来做到这一点,但如果我这样做,ajax函数被调用,但底层answerGroup对象的反馈属性不会更新。
我正在使用淘汰赛1.2.1。下面是javascript代码,我没有包含HTML。
我是以这种错误的方式去做的,还是仅仅是我的knockout.js事件绑定语法不正确?
任何人都可以帮忙吗?
<script>
var viewModel = {}
$(function() {
viewModel.scenarioId = ko.observable($("#Scenario_ScenarioID").val());
viewModel.answerGroups = ko.observableArray([]);
viewModel.addGroup = function (answerGroup) {
// add item to beginning of array
this.answerGroups.unshift(answerGroup);
};
ko.applyBindings(viewModel);
});
function answerGroup() {
this.id = ko.observable();
this.name = ko.observable();
this.feedback = ko.observable();
// the groups feedback has been updated so save
// these details back to the server
this.updateGroup = function (event) {
// javascript api library that is an ajax function.
// this works without a problem.
api.updateAnswerGroup({
success: function (result) {
alert("saved!");
},
error: function (e) {
alert("error!");
},
data: "answerGroupId=" + this.id + "&feedback=" + this.feedback
});
return true;
};
}
</script>
<script id="answerGroupsTemplate" type="text/html">
<div>
<h4><a href='#'>${ $data.name }</h4>
<div>
<textarea cols="100" rows="2" data-bind="event: { text: feedback, change: updateGroup }">
</textarea>
</div>
</div>
</script>
也许我懒,但我认为它太多的代码。哪一部分是相关部分 – Ibu 2011-06-05 18:47:44
伊布 - 所有这些都是相关的,我没有列入不相关的部分。 – Robini 2011-06-05 20:38:40
updateGroup函数被调用,但反馈属性的基础值未更新。我需要反馈属性来更新,然后调用updateGroup函数,以便我可以将此属性的值传递回服务器。 – Robini 2011-06-05 20:41:28