2014-07-24 33 views
0

我在showallinstructors.cshtml裏面寫了如下代碼。我的jQuery模板不起作用

<h2>Instructors List</h2> 
<table border="1"> 
    <thead> 
     <tr> 
      <th>Name</th> 
      <th>Hire Date</th> 
      <th>Twitter</th> 
      <th>Technologies</th> 
     </tr> 
    </thead> 
    <tbody id="instructorsList"> 
     @*Insert instructors here*@ 
    </tbody> 
</table> 

和內部_Layout.cshtml的文件

<head> 
//I have omitted the default Render code 
<script type="text/javascript"> 

var instructors = [ 
    { FirstName: "Matt", HireDate: new Date(2005, 6, 1), Twitter: "milnertweet", FavoriteTech: [{ Name: "jQuery" }, { Name: "WF" }, { Name: "BizTalk" }] }, 
    { FirstName: "Fritz", HireDate: new Date(2004, 8, 1), Twitter: "fritzonion", FavoriteTech: [{ Name: "ASP.NET" }, { Name: "JavaScript" }, { Name: "CSS" }] }, 
    { FirstName: "Aaron", HireDate: new Date(2004, 8, 1), Twitter: "skonnard" } 
]; 

$(function() { 
    $("#instructorTemplate").tmpl(instructors).append("#instructorsList"); 
}); 
</script> 

<script id="instructorTemplate" type="text/x-jquery-tmpl"> 
    <tr> 
     <td>${FirstName}</td> 
     <td>${HireDate}</td> 
     <td>${Twitter}</td> 
     <td>${Technologies}</td> 
    </tr> 
</script> 
</head> 

我期望使用jQuery模板以表格形式被顯示的輸出下面的代碼。

我什麼也沒有,只是表標題

因此,我已驗證視圖源。但是,我不明白問題在哪裏。

任何人都可以請建議我,我在哪裏做錯了。我沒有任何人提出除了我的計算器。

編輯: 當我調試了代碼後,我發現所有的導師都進入了「導師」變量。但是,只是不填充。我不知道如何去做。

+0

你使用的是什麼插件? TMPL? – Lakshay

+0

是的。我正在使用tmpl模板 –

+0

感謝您的投票,甚至沒有告知原因。無論如何,經過一番嘗試,我只給出瞭解決方案。 –

回答

2

非常小的錯誤。

上述代碼應爲:

$(function() { 
    $("#instructorTemplate").tmpl(instructors).appendTo("#instructorsList"); 
}); 

我已經取代追加appendTo。而已。