Skip to content

Instantly share code, notes, and snippets.

@Buildstarted
Created June 30, 2012 06:02
Show Gist options
  • Save Buildstarted/3022546 to your computer and use it in GitHub Desktop.
Save Buildstarted/3022546 to your computer and use it in GitHub Desktop.
Creates a menu item wrapped in an li tag
// Sample call
// @Html.MenuItem("Logs", new { controller = "logs", action = "index" })
public static class HtmlExtensionMethods
{
/// <summary>
/// Creates a new menu item (li)
/// </summary>
/// <param name="helper">class this extension method is attached to</param>
/// <param name="linkText">Link text to be output in the anchor tag</param>
/// <param name="routeValues">action/controller are required</param>
/// <returns>IHtmlString containing the html output</returns>
public static IHtmlString MenuItem(this HtmlHelper helper, string linkText, object routeValues)
{
return MenuItem(helper, linkText, routeValues, null);
}
/// <summary>
/// Creates a new menu item (li)
/// </summary>
/// <param name="helper">class this extension method is attached to</param>
/// <param name="linkText">Link text to be output in the anchor tag</param>
/// <param name="routeValues">action/controller are required</param>
/// <param name="htmlAttributes">Additional html attributes attached to the resulting list item</param>
/// <returns>IHtmlString containing the html output</returns>
public static IHtmlString MenuItem(this HtmlHelper helper, string linkText, object routeValues, object htmlAttributes)
{
return MenuItem(helper, linkText, new RouteValueDictionary(routeValues), htmlAttributes);
}
/// <summary>
/// Creates a new menu item (li)
/// </summary>
/// <param name="helper">class this extension method is attached to</param>
/// <param name="linkText">Link text to be output in the anchor tag</param>
/// <param name="routeValues">action/controller are required</param>
/// <param name="htmlAttributes">Additional html attributes attached to the resulting list item</param>
/// <returns>IHtmlString containing the html output</returns>
private static IHtmlString MenuItem(this HtmlHelper helper, string linkText, RouteValueDictionary routeValues, object htmlAttributes)
{
var liTag = CreateMenuListItem(helper, linkText, routeValues, htmlAttributes);
return new HtmlString(liTag.ToString());
}
/// <summary>
/// Creates a new menu item (li)
/// </summary>
/// <param name="helper">class this extension method is attached to</param>
/// <param name="linkText">Link text to be output in the anchor tag</param>
/// <param name="routeValues">action/controller are required</param>
/// <param name="htmlAttributes">Additional html attributes attached to the resulting list item</param>
/// <returns>A tagbuilder containing the li/anchor tag</returns>
private static TagBuilder CreateMenuListItem(HtmlHelper helper, string linkText, RouteValueDictionary routeValues, object htmlAttributes)
{
TagBuilder liTag = new TagBuilder("li");
liTag.MergeAttributes(new RouteValueDictionary(htmlAttributes));
string controller = ((string)routeValues["controller"]).ToLower();
string selectedMenu = helper.ViewBag.SelectedMenuItem;
if (!string.IsNullOrWhiteSpace(selectedMenu) && selectedMenu == controller)
{
if (liTag.Attributes.ContainsKey("class"))
{
liTag.Attributes["class"] = (liTag.Attributes["class"] += " selected ").Trim();
}
else
{
liTag.Attributes.Add("class", "selected");
}
}
liTag.InnerHtml = helper.ActionLink(linkText, (string)routeValues["action"], controller, null, new { @class = controller }).ToString();
return liTag;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment