淘汰赛:foreach不使用asp.net mvc

时间:2014-06-15 12:18:03

标签: asp.net-mvc knockout.js knockout-2.0 knockout-mvc

我知道这可能很容易,但不知怎的,我无法实现针对淘汰赛绑定的foreach。代码如下:

@{
    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>

呈现的HTML如下:

<!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>

问题在于foreach不像代码中实现的那样工作。我得到的错误是(使用Knockout上下文调试):

  

ExtensionError:TypeError message:&#34; Object.getOwnPropertyNames被调用   在非对象&#34; stack:(...)get stack:function(){[native code]}   set stack:function(){[native code]}    proto :错误信息:&#34;请选择带有ko数据的dom节点。&#34;

     

未捕获的TypeError:undefined不是函数

我已经尝试了一段时间,但没有成功。 感谢。

The binding is shown when I debug using knockout context debugger

1 个答案:

答案 0 :(得分:2)

所以我解决了!

问题是,淘汰赛2.0有一行代码:

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

但是我使用的jQuery 1.10弃用了clean方法。 所以我将我的淘汰赛升级到3.0并且它有效!

感谢@Boaz回答此问题question on stackoverflow