Hope
Hope

Reputation: 135

Knockout Not Working

I am new to web programming and am trying to implement Knockout Contact form to my website I built using MVC 4 Razor. I took the example straight from Knockout's website and tested my changes in JSFiddle; all is well. But when I bring the code into my cshtml it will not pick up the Knockout code. I am at a loss to what is going on. Any help? Details will be beneficial since there is much I do not know. There is a BeginForm helper after the Knockout contact grid on the cshtml. Is that an issue?

Javascript: AddTeamMember.js

var initialData = [
    {
        name: "Danny", email: "[email protected]", phone: "(555) 121-2121", dept: "Print"
    },
    {
        name: "Sensei", email: "[email protected]", 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" }))
...

Upvotes: 3

Views: 2375

Answers (2)

Phani Pulapa
Phani Pulapa

Reputation: 61

To activate Knockout, add the following line to a block:

 ko.applyBindings(myViewModel);

Upvotes: 0

alsafoo
alsafoo

Reputation: 788

This line has to be executed when your DOM is ready.

ko.applyBindings(new ContactsModel(initialData));

quoting from KO website:

To activate Knockout, add the following line to a block:

 ko.applyBindings(myViewModel);

You can either put the script block at the bottom of your HTML document, or you can put it at the top and wrap the contents in a DOM-ready handler such as jQuery’s $ function.

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

Upvotes: 6

Related Questions