在Mvc 4视图中未定义$

时间:2013-05-30 16:58:16

标签: jquery asp.net-mvc-4

我在JSConsole中收到错误,说$未定义。但是,当我查看页面源代码时,我看到包含了jQuery,但它包含在页面末尾。

BundleConfig

 public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                        "~/Scripts/jquery-ui-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.unobtrusive*",
                        "~/Scripts/jquery.validate*"));

            // Use the development version of Modernizr to develop with and learn from. Then, when you're
            // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));

            bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
                        "~/Content/themes/base/jquery.ui.core.css",
                        "~/Content/themes/base/jquery.ui.resizable.css",
                        "~/Content/themes/base/jquery.ui.selectable.css",
                        "~/Content/themes/base/jquery.ui.accordion.css",
                        "~/Content/themes/base/jquery.ui.autocomplete.css",
                        "~/Content/themes/base/jquery.ui.button.css",
                        "~/Content/themes/base/jquery.ui.dialog.css",
                        "~/Content/themes/base/jquery.ui.slider.css",
                        "~/Content/themes/base/jquery.ui.tabs.css",
                        "~/Content/themes/base/jquery.ui.datepicker.css",
                        "~/Content/themes/base/jquery.ui.progressbar.css",
                        "~/Content/themes/base/jquery.ui.theme.css"));
        }

_Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    @RenderBody()
    @Scripts.Render("~/bundles/jquery")
    @RenderSection("scripts", required: false)
</body>
</html>

视图

@model MvcWebAPI.Models.ComputerStatus

@{
    ViewBag.Title = "Index";
 }


 <script type="text/javascript">
     $(function () {
         alert('Hi');
     });
 </script>
 <h2>Index</h2>

呈现来源

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <link href="/Content/site.css" rel="stylesheet"/>

    <script src="/Scripts/modernizr-2.6.2.js"></script>

</head>
<body>

<script type="text/javascript">
    $(function () {
        alert('Hi');
    });
</script>
<h2>Index</h2>

    <script src="/Scripts/jquery-2.0.1.js"></script>


</body>
</html>

3 个答案:

答案 0 :(得分:2)

您的视图中的脚本似乎没有放在脚本部分中。确保将它们包装在

@section scripts
{
     <script>...</script>
}

答案 1 :(得分:0)

将您的身体改为此将解决问题

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

答案 2 :(得分:0)

确保_ViewStart指向:

@{
Layout = "~/Views/Shared/_Layout.cshtml";
}

如果是这种情况,那么您可以取出页面上的脚本标记。问题可能是文件未在文档就绪上加载,因此,您得到错误。如果您没有_ViewStart,只需确保您的视图指向_Layout进行样式化。

[编辑] 还可以尝试将脚本标记放在头部或文档末尾之外的任何其他位置。这是文件的加载,看看是否有帮助。

希望它有所帮助。