Skip to content

Instantly share code, notes, and snippets.

@danvacam
Created October 10, 2014 09:51
Show Gist options
  • Select an option

  • Save danvacam/a4a4f89541d467748a36 to your computer and use it in GitHub Desktop.

Select an option

Save danvacam/a4a4f89541d467748a36 to your computer and use it in GitHub Desktop.
razor html helper for action link with span
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