-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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