Created
October 10, 2014 09:51
-
-
Save danvacam/a4a4f89541d467748a36 to your computer and use it in GitHub Desktop.
razor html helper for action link with span
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
| public static class HtmlHelperExtensions | |
| { | |
| public static MvcHtmlString IconActionLink(this HtmlHelper htmlHelper, string linkText, string iconClass, | |
| string actionName, string controllerName) | |
| { | |
| return IconActionLink(htmlHelper, linkText, iconClass, string.Empty, actionName, controllerName, null); | |
| } | |
| public static MvcHtmlString IconActionLink(this HtmlHelper htmlHelper, string linkText, string iconClass, string actionName, string controllerName, IDictionary<string, object> htmlAttributes) | |
| { | |
| return IconActionLink(htmlHelper, linkText, iconClass, string.Empty, actionName, controllerName, null); | |
| } | |
| public static MvcHtmlString IconActionLink(this HtmlHelper htmlHelper, string linkText, string iconClass, | |
| string iconContent, | |
| string actionName, string controllerName) | |
| { | |
| return IconActionLink(htmlHelper, linkText, iconClass, iconContent, actionName, controllerName, null); | |
| } | |
| public static MvcHtmlString IconActionLink(this HtmlHelper htmlHelper, string linkText, string iconClass, string iconContent, | |
| string actionName, string controllerName, IDictionary<string, object> htmlAttributes) | |
| { | |
| string url = UrlHelper.GenerateUrl(null, actionName, controllerName, null, null, null, null, htmlHelper.RouteCollection, htmlHelper.ViewContext.RequestContext, true); | |
| TagBuilder tagBuilder = new TagBuilder("a") | |
| { | |
| InnerHtml = (!String.IsNullOrEmpty(linkText)) ? HttpUtility.HtmlEncode(linkText) : String.Empty | |
| }; | |
| tagBuilder.MergeAttributes(htmlAttributes); | |
| tagBuilder.MergeAttribute("href", url); | |
| TagBuilder iconTagBuilder = new TagBuilder("span"); | |
| iconTagBuilder.MergeAttribute("class", iconClass); | |
| iconTagBuilder.SetInnerText(iconContent); | |
| tagBuilder.InnerHtml += " " + iconTagBuilder.ToString(TagRenderMode.Normal); | |
| return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment