3

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

4
  • Why do you include knockout.js and knockout.mapping.js twice? Commented Jun 15, 2014 at 12:21
  • You have included knockout two times. Once in your layout and once in your view. Remove the <script src="~/Scripts/knockout.js"></script> <script src="~/Scripts/knockout.mapping-latest.js"></script> from your @section scripts! Commented Jun 15, 2014 at 12:22
  • @nemesv - I removed the scripts from the view but I'm still getting the same error. Commented Jun 15, 2014 at 12:35
  • @haim770 - I have updated the code but the problem is still there Commented Jun 15, 2014 at 12:37

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.