I start with the first MVC application. Here I have the main confusion. In fact, the default _Layout.cshtml file created as shown below
<!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> <script> </script> @RenderBody() @Scripts.Render("~/bundles/jquery") @RenderSection("scripts", required: false) </body> </html>
I used the base template, so I had no template. But check the line "@ Scripts.Render (" ~ / bundles / jquery ")". Its after @RenderBody (). Thus, it is actually added after the body section.
I think this is actually the best practice. But if I add $. (Document) .ready, it shows the following error
Microsoft JScript runtime error: '$' - undefined
Based on the error, this is due to the script tag. I just moved the line @ Scripts.Render ("~ / bundles / jquery") in front of the body of @Render and the last page, for example
<!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") @Scripts.Render("~/bundles/jquery") </head> <body> <script> </script> @RenderBody() @RenderSection("scripts", required: false) </body> </html>
The w application works fine with my jquery.
So why did this happen? So, do you always need a script add tag before @RenderBody? Then why is the default template displayed in the wrong place?
source share