Search code examples
asp.net-mvcknockout.jsknockout-2.0knockout-mvc

Knockout: foreach not working with asp.net mvc


I know this might be easy but somehow I'm unable to implement foreach for a knockout binding. The code is as below:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section scripts
{
    <script type="text/javascript">
        ko.applyBindings({
            people: [
                { firstName: 'Bert', lastName: 'Bertington' },
                { firstName: 'Charles', lastName: 'Charlesforth' },
                { firstName: 'Denise', lastName: 'Dentiste' }
            ]
        });
    </script>
}

    <div>
        <table>
            <thead>
                <tr><th>First name</th><th>Last name</th></tr>
            </thead>
            <tbody data-bind="foreach: people">
                <tr>
                    <td data-bind="text: firstName"></td>
                    <td data-bind="text: lastName"></td>
                </tr>
            </tbody>
        </table>
    </div>

And the rendered HTML is as follows:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> - My ASP.NET Application</title>
    <link href="/Content/bootstrap.css" rel="stylesheet"/>
<link href="/Content/site.css" rel="stylesheet"/>

    <script src="/Scripts/modernizr-2.6.2.js"></script>


</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="/">Application name</a>
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li><a href="/">Home</a></li>
                    <li><a href="/Home/About">About</a></li>
                    <li><a href="/Home/Contact">Contact</a></li>
                </ul>
            </div>
        </div>
    </div>
    <div class="container body-content">
          
    <div>
        <table>
            <thead>
                <tr><th>First name</th><th>Last name</th></tr>
            </thead>
            <tbody data-bind="foreach: people">
                <tr>
                    <td data-bind="text: firstName"></td>
                    <td data-bind="text: lastName"></td>
                </tr>
            </tbody>
        </table>
    </div>

        <hr />
        <footer>
            <p>&copy; 2014 - My ASP.NET Application</p>
        </footer>
    </div>

    <script src="/Scripts/jquery-1.10.2.js"></script>

    <script src="/Scripts/bootstrap.js"></script>
<script src="/Scripts/respond.js"></script>

    <script src="/Scripts/knockout.debug.js"></script>
<script src="/Scripts/knockout.mapping-latest.debug.js"></script>

    
    <script src="/Scripts/knockout.js"></script>
    <script src="/Scripts/knockout.mapping-latest.js"></script>
    <script type="text/javascript">
        ko.applyBindings({
            people: [
                { firstName: 'Bert', lastName: 'Bertington' },
                { firstName: 'Charles', lastName: 'Charlesforth' },
                { firstName: 'Denise', lastName: 'Dentiste' }
            ]
        });
    </script>

</body>
</html>

The problem is that the foreach doesn't work as implemented in the code. The error I get is (debugged using Knockout context):

ExtensionError: TypeError message: "Object.getOwnPropertyNames called on non-object" stack: (...) get stack: function () { [native code] } set stack: function () { [native code] } proto: Error info: "Please select a dom node with ko data."

Uncaught TypeError: undefined is not a function

I have trying this for a while now but with no success. Thanks.

The binding is shown when I debug using knockout context debugger


Solution

  • So I solved it!

    The problem was that knockout 2.0 has a line of code:

    var elems = jQuery['clean']([html]);

    But the jQuery 1.10 that I was using deprecated the clean method. So I upgraded my knockout to 3.0 and it worked!

    Thanks to @Boaz for answering this question on stackoverflow