I have section for scripts in my _Layout.cshtml:
<html>
<body>
...
@RenderSection("FooterScript", required: false)
</body>
</html>
I have view "Index.cshtml" that contains @Html.RenderPartial("LinksBlock", someModel)
.
LinksBlock partial requires script file "links.js". I want to make links.js inclusion into FooterScript from my partial view, not from main view (main view don't know about dependencies of partial view), and I want to be sure, that if I use more than 1 LinksBlock in my view, only 1 links.js was included. Is it possible?
Sections do not work with partial views. But you could write a pair of custom helpers that could be used in conjunction:
And then in your
_Layout.cshtml
:and in your partial: