2013-03-28 78 views
8

參照Link,我從MVC創建了一個UI對話框。
這裏是我的代碼:
在Layout.cshtml對象不支持屬性或方法對話框

<head> 
     <meta charset="utf-8" /> 
     <title>@ViewBag.Title - My ASP.NET MVC Application</title> 
     <link href="../../Content/smoothness/jquery-ui-1.10.2.custom.css" rel="stylesheet" type="text/css" /> 
     <script src="../../Scripts/jquery-1.9.1.js" type="text/javascript"></script> 
     <script src="../../Scripts/jquery-ui-1.10.2.custom.min.js" type="text/javascript"></script> 
     <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" /> 
     <meta name="viewport" content="width=device-width" /> 
     @Styles.Render("~/Content/css") 
     @Scripts.Render("~/bundles/modernizr") 
    </head> 

Index.cshtml

<h2>jQuery UI Hello World</h2>   
    <button id="show-dialog">Click to see jQuery UI in Action</button>    
    <div id="dialog" title="jQuery UI in ASP.NET MVC" > 
     <p>You now have the power of ASP.NET, the simplicity of client-side scripting with jQuery, and the looks of jQuery UI. Congrats, web slinger!</p> 
    </div>  
    <script language="javascript" type="text/javascript"> 
     $(function() { 
      $('#dialog').dialog({ 
       autoOpen: false, 
       width: 600, 
       buttons: { 
        "Ok": function() { $(this).dialog("close"); }, 
        "Cancel": function() { $(this).dialog("close"); } 
       } 
      }); 
      $("#show-dialog").button().click(function() { 
       $('#dialog').dialog('open'); 
       return false; 
      }); 
     }); 
    </script>  

我都在IE和Firefox檢查。 Firefox拋出「Typeerror $(...)。對話框不是函數」和IE拋出對象不支持屬性或方法'對話框'
有任何建議。我的代碼中有什麼錯誤。爲什麼出現對話錯誤?

+1

爲什麼你的'Index.cshtml'使用WebForms語法?你所顯示的是一個無效的剃刀視圖。 – 2013-03-28 10:47:01

+0

是的。你是對的。請檢查我的編輯。 – kk1076 2013-03-28 10:52:15

回答

14

3事情浮現在腦海中,可能是值得一試:

  1. 決不硬編碼在ASP.NET MVC應用程序的URL。始終使用(這是推薦或束)助手:

    <head> 
        <meta charset="utf-8" /> 
        <title>@ViewBag.Title - My ASP.NET MVC Application</title> 
        <link href="~/Content/smoothness/jquery-ui-1.10.2.custom.css" rel="stylesheet" type="text/css" /> 
        <script src="~/Scripts/jquery-1.9.1.js" type="text/javascript"></script> 
        <script src="~/Scripts/jquery-ui-1.10.2.custom.min.js" type="text/javascript"></script> 
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" /> 
        <meta name="viewport" content="width=device-width" /> 
        @Styles.Render("~/Content/css") 
        @Scripts.Render("~/bundles/modernizr") 
    </head> 
    
  2. 確保在年底你_Layout.cshtml你沒有一個@Scripts.Render("~/bundles/jquery")調用,因爲這將包括jQuery的兩倍。

  3. 如果在年底你_Layout.cshtml你有喜歡@RenderSection("scripts", required: false)自定義腳本專用的部分,確保你已經放置在該節中的自定義腳本(請注意,因爲這RenderSection是在DOM你不結束「T需要,因爲它執行的時間是包裹在一個事件的document.ready你的腳本,在DOM就已經被加載):

    <h2>jQuery UI Hello World</h2>   
    <button id="show-dialog">Click to see jQuery UI in Action</button>    
    <div id="dialog" title="jQuery UI in ASP.NET MVC" > 
        <p>You now have the power of ASP.NET, the simplicity of client-side scripting with jQuery, and the looks of jQuery UI. Congrats, web slinger!</p> 
    </div>  
    
    @section scripts { 
        <script type="text/javascript"> 
         $('#dialog').dialog({ 
          autoOpen: false, 
          width: 600, 
          buttons: { 
           "Ok": function() { $(this).dialog("close"); }, 
           "Cancel": function() { $(this).dialog("close"); } 
          } 
         }); 
         $("#show-dialog").button().click(function() { 
          $('#dialog').dialog('open'); 
          return false; 
         }); 
        }); 
        </script> 
    } 
    
+0

非常棒..這個工程很酷......非常感謝.. – kk1076 2013-03-28 11:09:11

+0

@PankajGarg,我已經更新了答案,以便您可以回滾您的downvote。 – 2013-04-01 05:48:01

+0

我遇到了這個問題,發現我在部分視圖中留下了一些「

0

您的代碼似乎對我很好。您可以檢查您的jQuery UI自定義包含對話框功能(或者嘗試下載full jQuery UI用於測試目的)並檢查JS腳本的URI是否正確。

7

對我來說,這個錯誤是因爲我已經忘了添加jquery-ui文件參考:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js" type="text/javascript"></script> 
1

當您忘記添加jquery-ui.js時,通常會發生這種情況。包括jquery-ui-{version}.js的順序也很重要!

您應該包含jquery-{version}.js,然後jquery-ui-{version}.js。然後在</body>標記之前,包含您的自定義JavaScript文件。

這將解決JavaScript運行錯誤:對象不支持屬性或方法 '對話框'],['$' 是未定義]

+0

感謝您指出訂單的重要性 - 這就解決了我的問題。 – EJoshuaS 2017-03-01 21:59:45

0

包括那些三行代碼:

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css" /> 
<script src="//code.jquery.com/jquery-1.10.2.js" type="text/javascript"></script> 
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js" type="text/javascript"></script> 
相關問題