2014-06-15 135 views
3

工作,我知道這可能是容易的,但不知何故,我無法實現淘汰賽結合的foreach。代碼如下:淘汰賽:的foreach不與asp.net的MVC

@{ 
    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不起作用。我得到的是(調試使用淘汰賽上下文)的錯誤:

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

我現在,但沒有成功嘗試了一會兒。 謝謝。

The binding is shown when I debug using knockout context debugger

+0

爲什麼你'包括和knockout.js''knockout.mapping.js'兩次? – haim770

+0

你已經包括基諾**兩次**。一旦進入你的佈局,一旦進入你的視野。刪除' '從'@section scripts'! – nemesv

+0

@nemesv - 我從視圖中刪除了腳本,但我仍然收到相同的錯誤。 – CC7589

回答

2

所以我解決了它!

的問題是,敲除2.0有一行代碼:

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

但jQuery的1.10,我用棄用清潔方法。 所以我升級我的淘汰賽到3.0,它的工作!

感謝@Boaz回答這個question on stackoverflow