public static string NavigationLink(this HtmlHelper htmlHelper, string url, string innerHtml, object htmlAttributes, string wrapperElement, bool markSelectedWhenActive) { if (string.IsNullOrEmpty(url)) { return(string.Empty); } var tagBuilder = new TagBuilder("a"); tagBuilder.MergeAttribute("href", url); tagBuilder.AddAttributes(htmlAttributes); if (UrlMatchesCurrentPath(url, htmlHelper) && string.IsNullOrEmpty(wrapperElement)) { tagBuilder.MergeAttribute("class", "selected"); } tagBuilder.InnerHtml.Append(innerHtml); var navigationLink = tagBuilder.ToString(); //ToString(TagRenderMode.Normal); if (!string.IsNullOrEmpty(wrapperElement)) { var wrapperElementBuilder = new TagBuilder(wrapperElement); if (UrlMatchesCurrentPath(url, htmlHelper)) { wrapperElementBuilder.MergeAttribute("class", "selected"); } wrapperElementBuilder.InnerHtml.Append(navigationLink); navigationLink = wrapperElementBuilder.ToString(); //ToString(TagRenderMode.Normal); } return(navigationLink); }
public static string NavigationLink(this HtmlHelper htmlHelper, string url, string innerHtml, object htmlAttributes, string wrapperElement, bool markSelectedWhenActive) { if (string.IsNullOrEmpty(url)) return string.Empty; var tagBuilder = new TagBuilder("a"); tagBuilder.MergeAttribute("href", url); tagBuilder.AddAttributes(htmlAttributes); if (UrlMatchesCurrentPath(url, htmlHelper) && string.IsNullOrEmpty(wrapperElement)) { tagBuilder.MergeAttribute("class", "selected"); } tagBuilder.InnerHtml = innerHtml; var navigationLink = tagBuilder.ToString(TagRenderMode.Normal); if (!string.IsNullOrEmpty(wrapperElement)) { var wrapperElementBuilder = new TagBuilder(wrapperElement); if (UrlMatchesCurrentPath(url, htmlHelper)) { wrapperElementBuilder.MergeAttribute("class", "selected"); } wrapperElementBuilder.InnerHtml = navigationLink; navigationLink = wrapperElementBuilder.ToString(TagRenderMode.Normal); } return navigationLink; }
public static MvcHtmlString Navigation(this HtmlHelper htmlHelper, Func<IEnumerable<MvcHtmlString>> items, object attributes = null, string listElement = "ul") { var nonEmptyItems = items().Where(x => !String.IsNullOrWhiteSpace(x.ToHtmlString())); if (nonEmptyItems.Any() == false) return MvcHtmlString.Empty; var innerHtml = nonEmptyItems.Aggregate(String.Empty, (current, item) => String.Concat(current, item.ToHtmlString())); var list = new TagBuilder(listElement); list.AddAttributes(attributes); list.InnerHtml = innerHtml; return MvcHtmlString.Create(list.ToString()); }
public static MvcHtmlString NavigationLink(this HtmlHelper htmlHelper, string url, string innerHtml, string wrapperElement = null, object htmlAttributes = null, bool markSelectedWhenActive = true, bool markSelectedWhenExactMatchOnly = false, bool alwaysDisplayInnerHtml = false ) { if (string.IsNullOrEmpty(url)) return alwaysDisplayInnerHtml ? MvcHtmlString.Create(innerHtml) : MvcHtmlString.Empty; var tagBuilder = new TagBuilder("a"); tagBuilder.MergeAttribute("href", url); tagBuilder.AddAttributes(htmlAttributes); Func<string, HtmlHelper, bool> urlIsCurrentPathMatch = UrlMatchesCurrentPath; if (markSelectedWhenExactMatchOnly) urlIsCurrentPathMatch = UrlMatchesCurrentPathExactly; if (markSelectedWhenActive && urlIsCurrentPathMatch(url, htmlHelper) && string.IsNullOrEmpty(wrapperElement)) tagBuilder.InnerHtml = String.Format("<strong>{0}</strong>", innerHtml); else tagBuilder.InnerHtml = innerHtml; var navigationLink = tagBuilder.ToString(TagRenderMode.Normal); if (!string.IsNullOrEmpty(wrapperElement)) { var wrapperElementBuilder = new TagBuilder(wrapperElement); if (markSelectedWhenActive && urlIsCurrentPathMatch(url, htmlHelper)) wrapperElementBuilder.InnerHtml = String.Format("<strong>{0}</strong>", navigationLink); else wrapperElementBuilder.InnerHtml = navigationLink; navigationLink = wrapperElementBuilder.ToString(TagRenderMode.Normal); } return MvcHtmlString.Create(navigationLink); }
public static TagBuilder AddAttributes(this TagBuilder tagBuilder, object htmlAttributes) { return(tagBuilder.AddAttributes(htmlAttributes, false)); }