为什么Asp.Net MVC 5在_Layout.cshtml的底部放置@ Scripts.Render(“〜/ bundles/jquery”)?

我把< script>在使用模板的一个(而且只有一个)cshtml文件的主体中使用jQuery的块,并且它们导致错误,因为jQuery尚未加载。

把@ Scripts.Render(“〜/ bundles / jquery”)放在_Layout.cshtml文件的底部是什么意思?

底部的_Layout.cshtml。

@RenderBody()
    <hr />
    <footer>
    </footer>
</div>

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

下面显示cshtml文件的生成源。

<script>
    $(document).ready(function () { /// $ not defined.
        // .....
    });
</script>

    <hr />
    <footer>
    </footer>
</div>

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

解决方法

你可以使用部分:

在你的布局中:

...
<script src="/Scripts/jquery-1.10.2.js"></script>
<script src="/Scripts/bootstrap.js"></script>
@RenderSection("scripts",required: false)
...

在你的cshtml中:

@section scripts {
    <script>
        $(document).ready(function () { /// $ not defined.
            // .....
        });
    </script>
}

相关文章

判断URL文件是不是在于在。private static bool UrlIsExist(...
由于在.net中,Request时出现有HTML或Javascript等字符串时,...
public static bool ProcessIdCard(this string idCard, out...
protected void Page_Load(object sender, EventArgs e){ Sc...