In my MVC3 solution, I am attempting to implement the solution in this answer to dynamically add a CSS reference to the _Layout.cshtml file from within a partial view.
Here is a snippet of my _Layout file:
<!DOCTYPE html>
<html>
<head>
@Html.RenderStyles()
</head>
<body>
<div id="header-wrapper">
@Html.Action("Header", "Shared")
</div>
<div class="container">
@RenderBody()
</div>
@RenderSection("scripts", required: false)
</body>
</html>
and in the partial view (that is rendered by the @Html.Action("Header", "Shared") bit):
@{
Html.AddStyle(Model.Company.CSSPath);
}
While the HtmlExtension methods are working as expected, it seems that the events are occuring in the incorrect order.
The @Html.RenderStyles() in the _Layout head is occuring before the Partial View has time to add the CSS file to the Styles. I was under the impression that Partial Views were rendered before the _Layout.
I'm guessing that the cause has to do with the fact that I'm rendering my Partial View with an Html.Action call. If I don't do so, however, then how can I populate the Partial view with its desired model?
Per Matt Razza's comment, if I were to render the partial view the "normal way" using: @Html.Partial("ViewName", model) I'm then required to give my _Layout a Model and then force all other models to inherit from that Model? Is that truly the best option?