Umbraco 7 Set dynamic body class in body tag

How to set up a dynamic body class in body tag in an Umbraco project?

To make this work in the simplest way possible is using ViewBag

In your _Layout.cshtml   (or Master whatever you called it) Set the body tag as the below

 

         

<body class="@ViewBag.BodyClassName">

@Html.Partial("Navigation/TopNav")
<div class="wrapper">
@RenderBody()
</div>
</div>
</footer>
@RenderSection("FooterScripts", false)
</body>
</html>

Then in all the others views.cshtml

Example ContentPage.cshtml, LandingPage.cshtml

Add the below     ViewBag

         

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.ContentPage>
@using ContentModels = Umbraco.Web.PublishedContentModels;
@{
ViewBag.BodyClassName = "content-page";

}

Once done, go to any your content page and you will see

The body will look like this <body class=”content-page”>

Then you will set up your CSS like the below

So all the styles apply to the content page  template

         

body.content-page {

//your styles here

}

Useful   Link:

ViewBag in MVC