Skip to content

Instantly share code, notes, and snippets.

@cigano
Last active May 28, 2018 20:30

Revisions

  1. cigano revised this gist Sep 15, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions FontAwesomeActionLink
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    <!-- language: c# -->

    public static class HtmlExtensions
    {
    public static IHtmlString FontAwesomeActionLink(this HtmlHelper htmlHelper, string linkText,
  2. cigano revised this gist Sep 15, 2014. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion FontAwesomeActionLink
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,14 @@
    public static class HtmlExtensions
    {
    public static IHtmlString FontAwesomeActionLink(this HtmlHelper htmlHelper, string linkText,
    string actionName, string controllerName, string fontAwesomeClass, object routeValues, object htmlAttributes)
    string actionName, string fontAwesomeClass, object routeValues = null, object htmlAttributes = null)
    {
    return FontAwesomeActionLink(htmlHelper, linkText, actionName, null, fontAwesomeClass, routeValues,
    htmlAttributes);
    }

    public static IHtmlString FontAwesomeActionLink(this HtmlHelper htmlHelper, string linkText,
    string actionName, string controllerName, string fontAwesomeClass, object routeValues = null, object htmlAttributes = null)
    {
    var targetUrl = UrlHelper.GenerateUrl(null, actionName, controllerName, ObjectToDictionary(routeValues), htmlHelper.RouteCollection, htmlHelper.ViewContext.RequestContext, true);
    return new MvcHtmlString(GenerateLink(linkText, targetUrl, fontAwesomeClass, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)));
  3. cigano created this gist Sep 15, 2014.
    45 changes: 45 additions & 0 deletions FontAwesomeActionLink
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    public static class HtmlExtensions
    {
    public static IHtmlString FontAwesomeActionLink(this HtmlHelper htmlHelper, string linkText,
    string actionName, string controllerName, string fontAwesomeClass, object routeValues, object htmlAttributes)
    {
    var targetUrl = UrlHelper.GenerateUrl(null, actionName, controllerName, ObjectToDictionary(routeValues), htmlHelper.RouteCollection, htmlHelper.ViewContext.RequestContext, true);
    return new MvcHtmlString(GenerateLink(linkText, targetUrl, fontAwesomeClass, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)));
    }

    private static string GenerateLink(string linkText,
    string targetUrl,
    string fontAwesomeClass,
    IDictionary<string, object> htmlAttributes
    )
    {
    var a = new TagBuilder("a");

    a.MergeAttributes(htmlAttributes);
    a.MergeAttribute("href", targetUrl);

    var i = new TagBuilder("i");

    i.MergeAttribute("class", "fa " + fontAwesomeClass);
    a.InnerHtml = i.ToString(TagRenderMode.Normal) + " " + linkText;

    return a.ToString(TagRenderMode.Normal);
    }

    public static RouteValueDictionary ObjectToDictionary(object value)
    {
    var dictionary = new RouteValueDictionary();

    if (value != null)
    {
    foreach (PropertyInfo property in value.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
    .Where(prop => prop.GetIndexParameters().Length == 0 &&
    prop.GetMethod != null))
    {
    dictionary.Add(property.Name, property.GetValue(value));
    }
    }

    return dictionary;
    }
    }