2012-06-24 106 views
-1

我有以下代碼:JavaScript和MVC 3 Razor視圖引擎

<html> <head> 
     <!-- Required CSS --> <link type="text/css" rel="stylesheet" href="http://yui.yahooapis.com/2.9.0/build/treeview/assets/skins/sam/treeview.css"> <!-- Optional CSS for for date editing with Calendar--> <link type="text/css" rel="stylesheet" href="http://yui.yahooapis.com/2.9.0/build/calendar/assets/skins/sam/calendar.css"> <!-- Dependency source file --> <script src="http://yui.yahooapis.com/2.9.0/build/yahoo-dom-event/yahoo-dom-event.js" 
></script> <!-- Optional dependency source file --> <script src="http://yui.yahooapis.com/2.9.0/build/animation/animation-min.js" type="text/javascript"></script> <!-- Optional dependency source file for date editing with Calendar--> <script src="http://yui.yahooapis.com/2.9.0/build/calendar/calendar-min.js" 
></script> <!-- Optional dependency source file to decode contents of yuiConfig markup attribute--> <script src="http://yui.yahooapis.com/2.9.0/build/json/json-min.js" ></script> <!-- TreeView source file --> <script src="http://yui.yahooapis.com/2.9.0/build/treeview/treeview-min.js" 
></script> 
    </head> <body class="yui-skin-sam"> 

@model IEnumerable< ESimSol.BusinessObjects.MenuTreeNode>   

<input type="button" id="btnAdd" value="Refresh" /> <label id="lblTest"> </label> <div id="treeDiv1"> <ul>  
    @foreach (var item in Model) 
    { 
     if (item.ParentID == 0) 
     { 
      <li> 
       @Html.DisplayFor(modelItem => item.MenuName) 
       <ul> 
        @Html.Partial("TreeViewControl", item) 
       </ul> 
      </li> 
     } 
    } </ul> </div> </body> </html> 


<script type="text/javascript"> 

    $(document).ready(function() { 
     treeInit(); 
    }); 

    var tree; 
    function treeInit() { 
     tree = new YAHOO.widget.TreeView(document.getElementById("treeDiv1")); 
    } 

</script> 

當我要執行我的劇本我面對以下情況除外:

Microsoft JScript runtime error: Object expected 

任何一個給我的JavaScript的完整參考用MVC3 RAZOR查看引擎執行?

+0

看看你以前的問題,並接受解決你的問題的答案(通過選擇答案旁邊的勾號圖標)。 –

+0

謝謝Darren Davies –

回答

0

請記住,在MVC3/Razor中使用JavaScript與在任何其他頁面上使用JavaScript沒有什麼不同 - JS是客戶端語言,因此它始終使用呈現的HTML。

我的JS代碼中沒有看到任何突出的錯誤,但您的<script>元素應位於<body>元素內。大多數瀏覽器處理它確定,所以我懷疑這是你的問題。

我建議您檢查腳本引用以驗證它們是否正確,然後再查看腳本。你在IE中得到的腳本錯誤應該引用一行#,你可以用它來查看頁面源(渲染頁面,而不是剃鬚刀頁面)來嘗試找到錯誤的來源。