MVC 3 Set CSS class based on controller name

<%
//Response.Write(ViewContext.RouteData.GetRequiredString("controller"));
string currentController = ViewContext.RouteData.GetRequiredString("controller");
string activeClass = "background-color: #fff";

//....
%>
<ul id="menu">
	<li><a href="<%=Url.Content("~/")%>" <% if(ViewContext.RouteData.GetRequiredString("controller") == "Home") {%>style="background-color: #fff;" <% } %>> Home</a></li>
	<li><%: Html.ActionLink("Profile", "Index", new { controller = "Profile", action = "Index", id = -1 }, new { style = currentController == "Profile" ? activeClass : string.Empty })%></li>
	<li><%: Html.ActionLink("Reports", "Index", new { controller = "Reports", action = "Index", id = -1 }, new { style = currentController == "Reports" ? activeClass : string.Empty })%></li>
	<li><%: Html.ActionLink("Contact", "Index", new { controller = "Contact", action = "Index", id = -1 }, new { style = currentController == "Contact" ? activeClass : string.Empty })%></li>
</ul>

Leave a Reply

Your email address will not be published. Required fields are marked *