views:

62

answers:

2

In ASP.NET MVC, there are these snippets of html called view templates which appear when their matching data appears on the screen. For example, if you have a customer order and it has a vendor address, the vendor address view template shows up populated with data.

Unfortunately, these don't have access to "MasterPages" nor are aware of their CSS surroundings.

Instead of loading these up with style tags, is there any way to create partial CSS files that could work for that particular html snippet, a sort of in-line CSS style section?

It would be really nice to plop this down just before we render the partial view:

<style type="text/css">
    input { margin: .2em .2em;
            overflow: hidden;
            width: 18.8em; 
            height: 1.6em;
            border: 1px solid black;}
</style>

to have the 15 or so input fields in that particular Html snippet be formatted the same. These are swapped out, so the positions of the input fields change. This may also imply a CSS reset on each partial view.

+1  A: 

Why not have a css file that is referenced in the partial views or am I mising the point of the question?

griegs
Each view would have their own css file. It would be nice to keep the relevant css within the text of the partial view, rather than a separate text file for each. We wouldn't know which partial view would appear for a given html page.
Dr. Zim
Can you reference a CSS file about 1/2 way down the body of an html file? That would essentially be the same as what I describe, except I would like to keep the CSS in the same text file as the partial view (html snippets).
Dr. Zim
While you can include a style block inline in the body section of a HTML file, you shouldn't do so: it's not valid HTML if you do so, nor is it if you link it. What Ahmad describes below is probably more what you're looking for.
Stephen Orr
+2  A: 

I thing that an idea would be to include an additional content section in Head of the master page

<head runat="server">
    <title>
        <asp:ContentPlaceHolder ID="TitleContent" runat="server" />
    </title>
    <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
    <asp:ContentPlaceHolder ID="CssContent" runat="server" />
</head>

In doing so, you can then inject the specific styles for each view.

Edit: After posting I think the above is not what you are referring to. What you may consider is rolling your own ViewUserControl similar to http://stackoverflow.com/questions/319748/script-css-registration-helper-in-asp-net-mvc?

Ahmad
So if we have lets say 20 partial views (.ascx files) and each wants to add content to the html head, how would you do it? Something like (Panel) Master.FindControl("CssContent").someFunctionToAppendContent()?
Dr. Zim
@Dr Zim - I see that the comment below talks about .ascx controls, and I must admit that I missed that part so my answer may not be correct. The solution above will work for the `ViewPage` (if I am not mistaken). To paraphrase the question as I understand it now - I have multiple .ascx controls that all have unique CSS styles. The styles are are defined within each control file. I want the CSS to be part of the `Head` of the document and not scattered within the HTML of my page.
Ahmad
@Dr Zim - Another option would be to create your own t4 templates or have a `ViewModel` that contains a reference to css file which you can then include in the Head. I will update the answer based on your response..
Ahmad