淘汰不起作用

时间:2014-03-30 20:23:02

标签: javascript asp.net-mvc asp.net-mvc-4 razor knockout.js

我是网络编程的新手,我正在尝试将Knockout Contact表单应用到我使用MVC 4 Razor构建的网站上。我直接从Knockout的网站上看到了这个例子并测试了我在JSFiddle中的变化;一切都很好。但是当我将代码带入我的cshtml时,它将不会获取Knockout代码。我对正在发生的事情感到茫然。有帮助吗?细节将是有益的,因为有很多我不知道。在cshtml上的Knockout联系人网格之后有一个BeginForm帮助器。这是一个问题吗?

Javascript:AddTeamMember.js

var initialData = [
    {
        name: "Danny", email: "LaRusso@stars.come", phone: "(555) 121-2121", dept: "Print"
    },
    {
        name: "Sensei", email: "Miyagi@stars.com", phone: "(555) 432-3466", dept: "AMS"
    }
    ];

    var ContactsModel = function (contacts) {
        var self = this;
        self.contacts = ko.observableArray(ko.utils.arrayMap(contacts, function (contact) {
            return { name: contact.name, email: contact.email, phone: contact.phone, dept: contact.dept };
        }));

        self.addContact = function () {
            self.contacts.push({
                name: "",
                email: "",
                phone: "",
                dept: ""
            });
        };

        self.removeContact = function (contact) {
            self.contacts.remove(contact);
        };

        self.addPhone = function (contact) {
            contact.phones.push({
                type: "",
                number: ""
            });
        };

        self.removePhone = function (phone) {
            $.each(self.contacts(), function () { this.phones.remove(phone) })
        };

        self.save = function () {
            self.lastSavedJson(JSON.stringify(ko.toJS(self.contacts), null, 2));
        };

        self.lastSavedJson = ko.observable("")
    };

    ko.applyBindings(new ContactsModel(initialData));

CSHTML:

@{
    ViewBag.Title = "Register";
}
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/knockout-2.2.0.js" type="text/javascript"></script>
<script src="~/MyJS/AddTeamMember.js"></script>

<hgroup class="title">
    <h1>@ViewBag.Title.</h1>
    <h2>@ViewBag.Message</h2>
</hgroup>

<div class="registerForm">
    <div class='memInfoForm'> 

        <h2>Contacts</h2>
        <div id='contactsList'>
            <table class='contactsEditor'>
                <tr>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Phone</th>
                    <th>Dept</th>
                </tr>
                <tbody data-bind="foreach: contacts">
                    <tr>
                        <td>
                            <input data-bind='value: name' />
                            <div><a href='#' data-bind='click: $root.removeContact'>Delete</a></div>
                        </td>
                        <td><input data-bind='value: email' /></td>
                        <td><input data-bind='value: phone' /></td>
                        <td><input data-bind='value: dept' /></td>
                    </tr>
                </tbody>
            </table>
        </div>

        <p>
            <button data-bind='click: addContact'>Add a contact</button>
            <button data-bind='click: save, enable: contacts().length > 0'>Save to JSON</button>
        </p>

        <textarea data-bind='value: lastSavedJson' rows='5' cols='60' disabled='disabled'> </textarea>   

    </div>


    @using (Html.BeginForm("Register", "Register", FormMethod.Post, new { id = "registerForm" }))
...

2 个答案:

答案 0 :(得分:5)

当您的DOM准备就绪时,必须执行此行。

ko.applyBindings(new ContactsModel(initialData));
从KO网站引用:

  

要激活Knockout,请将以下行添加到块中:

 ko.applyBindings(myViewModel);
     

您可以将脚本块放在HTML的底部   文档,或者您可以将它放在顶部并将内容包装在一个   支持DOM的处理程序,例如jQuery的$ function。

http://knockoutjs.com/documentation/observables.html

答案 1 :(得分:0)

要激活淘汰赛,请将以下行添加到块中:

 ko.applyBindings(myViewModel);