Skip to content

Instantly share code, notes, and snippets.

@junxy
Forked from ChrisWay/HtmlHelpers.cs
Last active August 29, 2015 13:56
Show Gist options
  • Save junxy/8877477 to your computer and use it in GitHub Desktop.
Save junxy/8877477 to your computer and use it in GitHub Desktop.
bootstrap nav active link setting asp.net mvc htmlhelper ref: http://chrisondotnet.com/2012/08/setting-active-link-twitter-bootstrap-navbar-aspnet-mvc/
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Linq;
namespace Web.Extensions
{
public static class HtmlHelpers
{
public static MvcHtmlString MenuLink(this HtmlHelper htmlHelper, string linkText, string actionName,
string controllerName, bool onlyMatchCt = false, string[] relatedCts = null)
{
var currentAction = htmlHelper.ViewContext.RouteData.GetRequiredString("action");
var currentController = htmlHelper.ViewContext.RouteData.GetRequiredString("controller");
var builder = new TagBuilder("li")
{
InnerHtml = htmlHelper.ActionLink(linkText, actionName, controllerName).ToHtmlString()
};
if (controllerName == currentController || (relatedCts != null && relatedCts.Contains(currentController)))
if (actionName == currentAction || onlyMatchCt)
builder.AddCssClass("active");
return new MvcHtmlString(builder.ToString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment