Reputation: 163
All scripts worked fine when I had just a view. And when I tried to split it into layout and partial view - scripts no longer worked. Please answer the questions:
1) where links should be placed? In a view or in _layout? If in layout, so should I use
@Scripts.Render()
or just
<script src="~/Scripts/jquery-1.7.1.js"></script>
2) Where scripts should be placed? In a view or in _layout? In head or inside body?
Or can I do like this: place scripts in view in
@section Scripts{ }
and then in layout use
@RenderSection("Scripts", false)
??
Sorry for silly questions :)
Upvotes: 0
Views: 257
Reputation: 458
I think it depends on how the script is being used. JQuery, I put on the layout page, because I am probably going to use it on all pages. If a function is specific to a view or a partial, then I might make a separate script file for that view or partial.
I also prefer using @Scripts.Render because is will do the minification for me, just be sure you are not referencing .min files, because it ignores them. That way you can have javascript files you can read while debugging, and have the system minify them for you in your production environment.
Upvotes: 0
Reputation: 121
You can do the following in the _layout.cshtml
@Scripts.Render("~/bundles/bundlename")
where bundlename is the name of the bundle when you call RegisterBundles. Alternatively, you can do the following:
@RenderSection("scripts", required: false)
Also, for performance place your js bundles at the bottom of the page.
Upvotes: 1