Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>you could create a HtmlHelper to return the value, that way you can use it anywhere</p> <p>Create a “HtmlHelpers” folder in your project Create a static class Define a static method with the first parameter “this HtmlHelper helper“</p> <pre><code>using System; using System.Collections.Generic; using System.Web; using System.Web.Mvc; namespace ThisMVCApp.WebUI.HtmlHelpers { public static class HtmlHelperExtensions { public static string IfActive(this HtmlHelper helper, string controller, string action) { string classValue = ""; string currentController = helper.ViewContext.Controller.ValueProvider.GetValue("controller").RawValue.ToString(); string currentAction = helper.ViewContext.Controller.ValueProvider.GetValue("action").RawValue.ToString(); if (currentController == controller &amp;&amp; currentAction == action) { classValue = "youarehere"; } return classValue; } } } </code></pre> <p>Add this to your web.config file (the one in your Views folder!!)</p> <pre><code>&lt;system.web.webPages.razor&gt; &lt;host ... /&gt; &lt;pages pageBaseType="System.Web.Mvc.WebViewPage"&gt; &lt;namespaces&gt; ... &lt;add namespace="ThisMVCApp.WebUI.HtmlHelpers" /&gt; &lt;/namespaces&gt; &lt;/pages&gt; &lt;/system.web.webPages.razor&gt; </code></pre> <p>now you can just apply the above method as any class or attribute on buttons or any element like so :</p> <pre><code>&lt;nav&gt; &lt;ul id="menu"&gt; &lt;li class="@Html.IfActive("Home", "Index")"&gt;@Html.ActionLink("Home", "Index", "Home")&lt;/li&gt; &lt;li class="@Html.IfActive("Home", "About")"&gt;@Html.ActionLink("About", "About", "Home")&lt;/li&gt; &lt;/ul&gt; &lt;/nav&gt; </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