Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In a recent project of mine I did it using HtmlHelper extensions and getting data from the ViewContext.RouteData.Values collection.</p> <p>So building off a simple extension like this:</p> <pre><code>public static string OnClass(this HtmlHelper html, bool isOn) { if (isOn) return " class=\"on\""; return string.Empty; } </code></pre> <p>You can build up any number of combinations, e.g.</p> <p>Just testing the current action:</p> <pre><code>public static string OnClass(this HtmlHelper html, string action) { string currentAction = html.ViewContext.RouteData.Values["action"].ToString(); return html.OnClass(currentAction.ToLower() == action.ToLower()); } </code></pre> <p>Testing for a number of actions:</p> <pre><code>public static string OnClass(this HtmlHelper html, string[] actions) { string currentAction = html.ViewContext.RouteData.Values["action"].ToString(); foreach (string action in actions) { if (currentAction.ToLower() == action.ToLower()) return html.OnClass(true); } return string.Empty; } </code></pre> <p>Testing for action and controller:</p> <pre><code>public static string OnClass(this HtmlHelper html, string action, string controller) { string currentController = html.ViewContext.RouteData.Values["controller"].ToString(); if (currentController.ToLower() == controller.ToLower()) return html.OnClass(action); return string.Empty; } </code></pre> <p>Etc, etc.</p> <p>Then you simply call it in your view(s) like so</p> <pre><code>&lt;ul id="left-menu"&gt; &lt;!-- simple boolean --&gt; &lt;li &lt;%= Html.OnClass(something == somethingElse) %&gt;&gt;Blah&lt;/li&gt; &lt;!-- action --&gt; &lt;li &lt;%= Html.OnClass("Index") %&gt;&gt;Blah&lt;/li&gt; &lt;!-- any number of actions --&gt; &lt;li &lt;%= Html.OnClass(new string[] { "Index", "Details", "View" }) %&gt;&gt;Blah&lt;/li&gt; &lt;!-- action and controller --&gt; &lt;li &lt;%= Html.OnClass("Index", "Home") %&gt;&gt;Blah&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>Which ever way you look at it, HtmlHelper extensions are your friend! :-)</p> <p>HTHs<br> Charles</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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