Putting an ID on <body> in ASP.NET MVC

asp.net-mvc, css, view

Solution

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>

Problem

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?

Original source