0

In an mvc 3 razor project I have a helper which creates a component. I can use this helper to create as many components as I need in the same page.

I have different folders containing css files and their images.

Can I specify the css style of each component from the helper?

i.e @html.MyComponent(100, 200, "pink") will uses the style.css in pink folder.

Ps: I am not using html5 neither css3

StuperUser
  • 10,555
  • 13
  • 78
  • 137
mehdouch
  • 423
  • 1
  • 8
  • 20

3 Answers3

1

If you would use classes instead of files it would be much easier. I would just use different styles for themes. You should look at this question: ASP.NET MVC 3, how to do themes right

Community
  • 1
  • 1
Tomas Jansson
  • 22,767
  • 13
  • 83
  • 137
0

ASP.NET MVC 3 Razor: Include JavaScript file in the head tag

I think the same thing can be applied but I don't know if you can do it from a helper.

Community
  • 1
  • 1
auo
  • 572
  • 1
  • 7
  • 16
  • thank you for your answer but this doesn't help me much. in each css file I have a MyComponent class but its attributes are not the same (color, background ...) I can' load all the css in the same time if I have many instance of the component in the same time. – mehdouch Mar 12 '12 at 15:01
0

If you are set on doing it this way - then

  1. You need to select the css file at the top for pink
  2. You need to include all style sheets in loading.
  3. You need to dynamically include style sheets when requested by MyComponent. This is tough as you may end up double including them. You can accomplish this via an ActionFilter to write out the css tags at the end, but this is a hack and I wouldn't recommend it.

Stick with convention and your styles should be requested at the top, so you need to know which styles you are using on the page. Your components shouldn't care about loading a style sheet, it should already be loaded which means you have to make this decision at the top of your page. Since you should already 'know' the names at this point (pink, etc) you can easily write the code at the top to request these files via a simply


<LINK href="@string.Format("/{0}/style.css",YourStyleSheetnameIePinkInThisExample)" rel="stylesheet" type="text/css">

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71