tags:

views:

33

answers:

1

I'm looking to set the <body> class as well based on the view.

I've read a few posts about strongly typing the master page but none seem to exactly fit, or at least I don't know how to code it.

I have noticed that Stack Overflwow does it, so it must be possible.

Updated:

Getting error: Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1061: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'Body' and no extension method 'Body' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?) on the body line

namespace MySite.Helpers
{
    public static class HtmlHelpers
    {
        public static MvcHtmlString Body(this HtmlHelper htmlHelper)
        {
            string currentControllerName = (string)htmlHelper.ViewContext.RouteData.Values["controller"];
            string currentActionName = (string)htmlHelper.ViewContext.RouteData.Values["action"];
            string css = currentControllerName + "-" + currentActionName;
            var body = new TagBuilder("body");
            body.AddCssClass(css);
            return MvcHtmlString.Create(body.ToString(TagRenderMode.StartTag));
        }
    }

}

~/Views/Shared/Site.Master:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
    <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
</head>

<body id="argh" class="<%= Html.Body() %>">
+2  A: 

You could use a helper method:

public static MvcHtmlString Body(this HtmlHelper htmlHelper)
{
    string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
    string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];
    string css = GetCss(currentControllerName, currentActionName);
    var body = new TagBuilder("body");
    body.AddCssClass(css);
    return MvcHtmlString.Create(body.ToString(TagRenderMode.StartTag));
}

And then in your master page:

<%= Html.Body() %>
    ...
</body>
Darin Dimitrov
What is GetCss()?
ryan
A function that you have to write and which decides which CSS class to use based on the current controller and action.
Darin Dimitrov
Thanks Darin. Until Justin updates the OP please check out the error im getting. http://efficient.pastebin.com/cSXkY1rc
ryan
It turns out I had to import the namespace in my master page. I would have thought my extension method would be available without this. <%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %><%@ Import Namespace="MySite.Helpers" %>
ryan