Layout pages head:
<head>
<link href="@Url.Content("~/Content/themes/base/Site.css")"
rel="stylesheet" type="text/css" />
</head>
A View (AnotherView) from the application needs:
<link href="@Url.Content("~/Content/themes/base/AnotherPage.css")"
rel="stylesheet" type="text/css" />
and AnotherView has a partial view (AnotherPartial) which needs:
<link href="@Url.Content("~/Content/themes/base/AnotherPartial.css")"
rel="stylesheet" type="text/css" />
Question: How can we add these CSS files links AnotherView and AnotherPartial links to Layout head?
RenderSection is not a good idea because AnotherPage can have more than one Partials. Add all CSS to head is not useful because it will change dynamicaly (it depends on Anotherpages).
You can define the section by RenderSection method in layout.
Layout
Then you can include your css files in section area in your view except partial view.
The section work in view, but not work in partial view by design.
If you really want to using section area in partial view, you can follow the article to redefine RenderSection method.
Razor, Nested Layouts and Redefined Sections – Marcin On ASP.NET
I wrote an easy wrapper that allows you to register styles and scrips in every partial view dynamically into the head tag.
It is based on the DynamicHeader jsakamoto put up, but it has some performance improvements & tweaks.
It is very easy to use, and versatile.
The usage:
You can find the full code, explanations and examples inside: Add Styles & Scripts Dynamically to Head Tag
For those of us using ASP.NET MVC 4 - this may be helpful.
First, I added a BundleConfig class in the App_Start folder.
Here’s my code that I used to create it:
Second, I registered the BundleConfig class in the Global.asax file:
Third, I added style helpers to the my CSS file:
Finally I used this syntax in any View:
I tried to solve this issue.
My answer is here.
"DynamicHeader" - http://dynamicheader.codeplex.com/, https://nuget.org/packages/DynamicHeader
For example, _Layout.cshtml is:
And, you can register .js and .css files to "DynamicHeader" anywhere you want.
For exmaple, the code block in AnotherPartial.cshtm is:
Then, finaly output HTML is:
Try the out-of-the-box solution (ASP.NET MVC 4 or later):
Sadly, this is not possible by default to use
section
as another user suggested, since asection
is only available to the immediatechild
of aView
.What works however is implementing and redefining the
section
in every view, meaning:This way every view can implement a head section, not just the immediate children. This only works partly though, especially with multiple partials the troubles begin (as you have mentioned in your question).
So the only real solution to your problem is using the
ViewBag
. The best would probably be a seperate collection (list) for CSS and scripts. For this to work, you need to ensure that theList
used is initialized before any of the views are executed. Then you can can do things like this in the top of every view/partial (without caring if theScripts
orStyles
value is null:In the layout you can then loop through the collections and add the styles based on the values in the
List
.I think it's ugly, but it's the only thing that works.
******UPDATE**** Since it starts executing the inner views first and working its way out to the layout and CSS styles are cascading, it would probably make sense to reverse the style list via
ViewBag.Styles.Reverse()
.This way the most outer style is added first, which is inline with how CSS style sheets work anyway.