iNerd Blog

ASP.NET MVC Menu Item Helper (selected/non-selected menu)

Posted in Uncategorized by Ari on 9 April 2010
using System;
using System.Web.Mvc;
using System.Web.Mvc.Html;
namespace Helpers
{
     /// <summary>
     /// This helper method renders a link within an HTML LI tag.
     /// A 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.
   /// </summary>
 public static class MenuItemHelper
 {
          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 builder = new TagBuilder("li");
 
                     // Add selected class
                     if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName,StringComparison.CurrentCultureIgnoreCase))
                            builder.AddCssClass("selected");            
 
                    // Add link
                   builder.InnerHtml = helper.ActionLink(linkText, actionName, controllerName);

                   // Render Tag Builder
                   return builder.ToString(TagRenderMode.Normal);
 }

         }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: