Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Expanding on Darin's example, here's the full class which adds additional optional parameters for RouteValues and HtmlAttributes on the helper. In effect, it behaves just like the base ActionLink.</p> <pre><code>using System; using System.Web.Mvc; using System.Web.Mvc.Html; namespace MYNAMESPACE.Helpers { public static class MenuExtensions { public static MvcHtmlString MenuItem(this HtmlHelper htmlHelper, string text, string action, string controller, object routeValues = null, object htmlAttributes = null) { var li = new TagBuilder("li"); var routeData = htmlHelper.ViewContext.RouteData; var currentAction = routeData.GetRequiredString("action"); var currentController = routeData.GetRequiredString("controller"); if (string.Equals(currentAction, action, StringComparison.OrdinalIgnoreCase) &amp;&amp; string.Equals(currentController, controller, StringComparison.OrdinalIgnoreCase)) { li.AddCssClass("active"); } if (routeValues != null) { li.InnerHtml = (htmlAttributes != null) ? htmlHelper.ActionLink(text, action, controller, routeValues, htmlAttributes).ToHtmlString() : htmlHelper.ActionLink(text, action, controller, routeValues).ToHtmlString(); } else { li.InnerHtml = htmlHelper.ActionLink(text, action, controller).ToHtmlString(); } return MvcHtmlString.Create(li.ToString()); } } } </code></pre> <p>And in the View folder's web.config:</p> <pre><code>&lt;system.web.webPages.razor&gt; &lt;host ... /&gt; &lt;pages ... &gt; &lt;namespaces&gt; ... ... &lt;add namespace="MYNAMESPACE.Helpers" /&gt; &lt;/namespaces&gt; &lt;/pages&gt; &lt;/system.web.webPages.razor&gt; </code></pre>
    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. This table or related slice is empty.
    1. This table or related slice is empty.
    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