Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to do it all server-side, I've done this before. Create an action filter attribute:</p> <pre><code>public class PageOptionsAttribute : ActionFilterAttribute { public string Title { get; set; } public string Section { get; set; } public override void OnActionExecuting(ActionExecutingContext filterContext) { var controller = filterContext.Controller as ControllerBase; if (controller != null) { controller.SetPageSection(this.Section); controller.SetPageTitle(this.Title); } base.OnActionExecuting(filterContext); } } </code></pre> <p>This calls two methods in my ControllerBase class that all my controllers inherit from:</p> <pre><code>public class ControllerBase : Controller { public void SetPageSection(string section) { // use the section defined or the controller name if none ViewData["PageSection"] = section != null ? section : this.RouteData.Values["controller"].ToString(); } public void SetPageTitle(string title) { ViewData["PageTitle"] = title; } } </code></pre> <p>Set the title and page section on you controller methods:</p> <pre><code>public class HomeController : ControllerBase { [PageOptions(Title="Home Page", Section="Home")] public ActionResult Index() { } } </code></pre> <p>Then I call the ViewData value from my master page (this won't interfere with ViewData.Model):</p> <pre><code>&lt;body class="&lt;%=ViewData["PageSection"] %&gt;"&gt; </code></pre> <p>Then to reference via CSS, instead of calling .current, give each nav item an ID and then use the body class in combination with that ID to determine the current page.</p> <pre><code>body.home #HomeNav { /* selected */ } body.about #AboutNav { /* selected */ } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload