JQuery After Body ASP .NET MVC 4

Viewed 12743

I just created an ASP .NET MVC 4 WebAPI project.

In looking at the default _Layout.cshtml, I see that the jquery script is being inserted after the body is rendered (not in the head).

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

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

This causes the error

$ is not defined 

of course, trying to write

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

Moving the

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

into the head section of the _Layout.cshtml corrects the problem and jquery works as expected.

What's up? Am I doing something wrong and there's a reason for the default location of the Script.Render inside _Layout.cshtml?

3 Answers
Related