Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I include css files from an MVC partial control?

I'm using ASP.NET MVC and I have a partial control that needs a particular CSS & JS file included. Is there a way to make the parent page render the script and link tags in the 'head' section of the page, rather than just rendering them inline in the partial contol?

To clarify the control that I want to include the files from is being rendered from a View with Html.RenderPartial and so cannot have server-side content controls on it. I want to be able to include the files in the html head section so as to avoid validation issues.

like image 348
Glenn Slaven Avatar asked Dec 15 '08 23:12

Glenn Slaven


1 Answers

If I have requirements for CSS/Javascript in a partial view, I simply make sure that any page that may include the partial view, either directly or as content retrieved from AJAX, has the CSS/Javascript included in it's headers. If the page has a master page, I add a content placeholder in the master page header and populate it in the child page. To get intellisense in the partial view, I add the CSS/Javascript includes in the partial view but wrapped with an if (false) code block so they are not included at runtime.

<% if (false) { %>
   <link href=...
   <script type=...
<% } %>
like image 183
tvanfosson Avatar answered Sep 20 '22 13:09

tvanfosson