views:

147

answers:

2

I would like my views to be able to specify a class for the <body> tag, which lies in my master page.

My first take was to do something like this:

<asp:Content ContentPlaceHolderID="BodyClassContent" runat="server">
    view-item</asp:Content>

However, that would require this in the master page, which doesn't work:

<body class="<asp:ContentPlaceHolder ID="BodyClassContent" runat="server" />">

Any solutions to this?

+2  A: 

Why don't you do in your masterpage:

<body class="<%=ViewData["bodyClass"].toString()%>">

and then set ViewData["bodyClass"] in your Controller actions? That should be equivalent...

Palantir
+2  A: 

I would suggest a different approach.

You create an hierachy of view models, starting with the MasterModel. When you instantiate a view object, you pass a body class to it.

public class MasterModel { string BodyCss { get; set; }

public MasterModel (string bodyCss)
{
    BodyCss = bodyCss;
}

}

public class MyView1Model : MasterModel : base ("body-view1") { }

Then in your master view which should be strongly typed to MasterView:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<MasterModel>" %>

you just write:

<body class="<%= Model.BodyCss %>"></body>
Developer Art
Good idea, but I would rather not have the class in my model – I want the view to solely responsible for layout, and thereby CSS.
Guðmundur H