我正在尝试将 knockout.js 与 MVC3 一起使用,但我不断收到错误消息:
未捕获的类型错误:无法调用未定义的方法“删除”
设置是我有一个 UL 列表,我需要添加和删除:
<ul data-bind="foreach: Interviewees">
<li>
<div>
<a data-bind="click: $root.removeInterviewee" class="xOut"></a>
</div>
<div>
<h2>
<span data-bind="text: FirstName"></span>
<span data-bind="text: LastName"></span>
</h2>
</div>
</li>
</ul>
这是包含淘汰赛内容的 javascript 部分:
function SomeThingee(Id, SomeThingeeId, firstName, lastName, title, email) {
this.Id = Id;
this.SomeThingeeId = SomeThingeeId;
this.FirstName = firstName;
this.LastName = lastName;
this.Title = title;
this.Email = email;
}
var viewModel = ko.validatedObservable({
addSomeThingee: function () {
if (!viewModel.isValid()) {
viewModel.errors.showAllMessages();
return false;
} else {
var newSomeThingee = new SomeThingee(this.Id(), 0, this.FirstName(), this.LastName(), this.Title(), this.Email());
$.ajax({
url: '@Url.Action("AddSomeThingee")',
type: "POST",
data: ko.toJSON(newSomeThingee),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (result) {
newSomeThingee.SomeThingeeId = result.message;
},
error: function (result) {
}
});
this.SomeThingees.push(newSomeThingee);
}
},
removeSomeThingee: function (item) {
this.SomeThingees.remove(item);
}
});
$(function () {
var jsonModel = '@Html.Raw(JsonConvert.SerializeObject(this.Model))';
var mvcModel = ko.mapping.fromJSON(jsonModel);
var myViewModel = new viewModel();
var g = ko.mapping.fromJS(myViewModel, mvcModel);
ko.applyBindings(g, document.getElementById("someDiv"));
});
此行发生错误:
this.SomeThingees.remove(item);
注意 SomeThingees 集合是由模型本身提供的。add 方法工作得很好,但 remove 方法不起作用,并给我上面列出的错误。我究竟做错了什么?