System.Web.Mvc.HtmlHelperExtension.MenuItem C# (CSharp) Method

MenuItem() public static method

This helper method renders a link within an HTML LI tag. A class="selected" attribute is added to the tag when the link being rendered corresponds to the current controller and action. This helper method is used in the Site.Master View Master Page to display the website menu.
public static MenuItem ( this helper, string linkText, string actionName, string controllerName ) : string
helper this
linkText string
actionName string
controllerName string
return string
        public static string MenuItem(this HtmlHelper helper, string linkText, string actionName, string controllerName)
        {
            string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
              string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

              var sb = new StringBuilder();

              if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase))
            sb.Append("<li class=\"selected\">");
              else
            sb.Append("<li>");

              sb.Append(helper.ActionLink(linkText, actionName, controllerName));
              sb.Append("</li>");
              return sb.ToString();
        }
HtmlHelperExtension