Reputation: 3109
First MVC project, just curious what the best way to manage this is.
All of my views that I'm creating contain the following
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
I would prefer to keep my js before my closing body tag. I know I can obviously move this to my layout.cshtml file, but I imagine there's a more elegant MVC/Razor way to do this (especially if I have view-specific files that I don't want to load globally).
How can I accomplish this?
Upvotes: 3
Views: 6943
Reputation: 16960
I put my global scripts inside _Layout.cshtml, and then define a section
for scripts I only want to include from certain views.
E.g. in layout.cshtml
<body>
...
<script src="@Url.Content("~/Scripts/global1.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/global2.js")" type="text/javascript"></script>
@RenderSection("FooterScripts", false)
</body>
</html>
and then in the views if I need a script I don't want to include globally:
@section FooterScripts
{
<script src="@Url.Content("~/Scripts/local.js")" type="text/javascript"></script>
}
Upvotes: 5