views:

301

answers:

3

I would like to create in my master page a control that depending on the rendered viewpage will change a css file

I was thinking to make a specific controller "CssController" and call this CssController from the src attribute of the css link. This controller will be in charge of choosing the right css and sending it back with text/css header.

question: will this approach break the mvc pattern? Indeed the choose of the design would be in charge of a controller. Is there a better alternative?

+1  A: 

The basic MVC example sets Page Title via the controller. I'm not sure this is any different. If you were using it (for instance) to have users be able to select different skins for their experience, I'm not sure there is any other way to do it, regardless of whether or not it violates the pattern.

Russell Steen
+1  A: 

If you are using separate css files for separate themes, then those would be in fact the view and controller would work. So it does not seem to violate the pattern. But, it does seem to be a more complicated solution than others.

The common way I have seen to do this is with a Jquery Style Switcher


http://www.cssnewbie.com/simple-jquery-stylesheet-switcher/


With a little bit of code you can determine the default style to show on each page

Hurricanepkt
No, it is not just for theming. Per example, in home page i want to have the css of site css and in my cart page the compiled content of site.css+cart.css. I will take a look at your link thanks.
Gregoire
+1  A: 

What i did in my master page was create a contentplaceholder in the <head> section, as so:

<head runat="server">
<title>Title Here</title>
<script src="<%= Url.Content("~/Scripts/jquery-1.3.2.min.js") %>" type="text/javascript" ></script>
<link href="<%= Url.Content("~/Content/css/site.css") %>" rel="stylesheet" type="text/css"  />
<asp:ContentPlaceHolder ID="HeadContent" runat="server" />
</head>

then, when you create a view using the master page, you can add the view-specific css you want into that content place holder. That way, it'll put your site.css first, plus the desired css, javascript, <script> blocks, whatever, on that particular view.

Jamie M