asp.net-mvc-3 - 未捕获的类型错误 : Cannot call method 'remove' of undefined

标签 asp.net-mvc-3 knockout.js knockout-mapping-plugin knockout-2.0

我尝试将 knockout.js 与 MVC3 一起使用,但不断收到错误:

Uncaught TypeError: Cannot call method 'remove' of undefined

设置是我需要添加和删除一个 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>

这是包含 knockout 内容的 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 集合是由模型本身提供的。添加方法工作得很好,但删除方法不起作用并给我上面列出的错误。我做错了什么?

最佳答案

问题是,当 click 绑定(bind)调用 $root.removeInterviewee 时,this 设置为数据项 ( $data)而不是 View 模型($root)。有几种方法可以解决这个问题。最简单的方法可能是在绑定(bind)中的函数引用上使用 bind

<a data-bind="click: $root.removeInterviewee.bind($root)" class="xOut"></a>

另请参阅此 Github issue供进一步讨论。

关于asp.net-mvc-3 - 未捕获的类型错误 : Cannot call method 'remove' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14247354/

相关文章:

javascript - 如何使用附加属性扩展映射的 knockout View 模型 "on the fly"?

c# - 是否可以在 application_start 中使用 fluent migrator?

javascript - Durandal 尝试从 viewmodel 文件夹访问 View

javascript - 使用knockout js绑定(bind)在动态文本框上绑定(bind)日期选择器

knockout.js - 对复杂的JSON使用Knockout映射

asp.net - upshot.js + 信号器 + knockout

asp.net-mvc - ASP.NET MVC 中的 ViewModel 实现 - 这段代码是最佳实践吗?

asp.net - View 模型上的类型更改后, Razor View 上出现 MissingMethodException

asp.net-mvc-3 - 动态显示选择列表下拉列表中选定的值

javascript - 映射到 <bean> 到 knockout .js