如何在asp.net MVC4查看页面中包含javascript代码?

时间:2012-10-17 10:43:42

标签: asp.net-mvc-4

我是asp.net MVC4架构的新手,我对以下事情感到困惑,请帮助我。

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryui")

<script type="text/javascript">
$(function () {
    $("#sections").tabs();
    //here i am getting error that Object[object object] has not method tabs    
});
</script>

<div id="sections">
    <ul>
        <li><a href="#section-1">section-1 </a></li>
        <li><a href="#section-2">section-2 </a></li>
    </ul>
        <div id="section-1">
        section-1 content............  
        </div>
        <div id="section-2">
        section-2 content............ 
        </div>
    </div>

感谢先生......

1 个答案:

答案 0 :(得分:10)

您可能已经将~/bundles/jquery捆绑包两次包含在内。查看您的~/Views/Shared/_Layout.cshtml文件。最后你可能有以下内容:

    @Scripts.Render("~/bundles/jquery")
    @RenderSection("scripts", required: false)
</body>

因此jQuery包已包含在您的页面中。您不应该在视图中第二次包含它。您只需要~/bundles/jqueryui捆绑包:

@Scripts.Render("~/bundles/jqueryui")

<script type="text/javascript">
$(function () {
    $("#sections").tabs();
    //here i am getting error that Object[object object] has not method tabs    
});
</script>

<div id="sections">
    <ul>
        <li><a href="#section-1">section-1 </a></li>
        <li><a href="#section-2">section-2 </a></li>
    </ul>
    <div id="section-1">
        section-1 content............  
    </div>
    <div id="section-2">
        section-2 content............ 
    </div>
</div>

更新:

以下是视图结构可能如下所示的完整示例:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Foo</title>
    <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" />
</head>
<body>
    <div id="sections">
        <ul>
            <li><a href="#section-1">section-1 </a></li>
            <li><a href="#section-2">section-2 </a></li>
        </ul>
        <div id="section-1">
            section-1 content............  
        </div>
        <div id="section-2">
            section-2 content............ 
        </div>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryui")
    <script type="text/javascript">
        $("#sections").tabs();
    </script>
</body>
</html>