63

I want to present a button with @Html.ActionLink but i need to have my application font in the text.

With this code:

<span>
    @Html.ActionLink("  Create New", "Create", null, new { @class = "btn btn-warning glyphicon glyphicon-plus-sign" })
</span>

I get my button but the Create New text appears with the glyphicon font-family.

Putting the glyphicon classes in the span doesn't change anything

e4rthdog
  • 5,103
  • 4
  • 40
  • 89

10 Answers10

149

You should not add the glyphicon class to the a-tag.

From the Bootstrap website:

Don't mix with other components Icon classes cannot be directly combined with other components. They should not be used along with other classes on the same element. Instead, add a nested <span> and apply the icon classes to the <span>.

Only for use on empty elements Icon classes should only be used on elements that contain no text content and have no child elements.

In other words the correct HTML for this to work the way you want would be: <a href="#" class="btn btn-warning">test <span class="glyphicon glyphicon-plus-sign"></span></a>

This makes the Html.ActionLink helper unsuitable. Instead you could use something like:

<a href="@Url.Action("Action", "Controller")" class="btn btn-warning">
    link text 
    <span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span>
</a>
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
Robban
  • 6,729
  • 2
  • 39
  • 47
23

This works for me in MVC 5:

@Html.ActionLink(" ", "EditResources", "NicheSites", new { ViewBag.dbc, item.locale, ViewBag.domainId, domainName = ViewBag.domaiName }, new {@class= "glyphicon glyphicon-edit" })

The first parameter cannot be empty or null or it will blow.

Norbert Norbertson
  • 2,102
  • 1
  • 16
  • 28
  • 1
    Thanks. Worked for me. – Vaibhav Oct 24 '16 at 10:21
  • This works for an ActionLink that doesn't have text, i.e., a close button, add button, subtract button, etc. Which is exactly what I was looking for. The "accepted" answer is the best way if your button has text. Thank you – Nicholas Jan 12 '17 at 19:53
  • Even if this works, it doesn't follow the Bootstrap docs, which state that the glyphicon class should not be combined with other classes or elements that contain text. – devlin carnate Mar 30 '17 at 21:37
9

It might be better to just write out the HTML rather than try to make it work with HtmlHelper.ActionLink...

<span>
    <a href="@Url.Action("Create")" class="btn btn-warning">
        <span class="glyphicon glyphicon-plus-sign"></span>
        Create New
    </a>
</span>
Anthony Chu
  • 37,170
  • 10
  • 81
  • 71
5

Here's mine. Inspired by Andrey Burykin

public static class BensHtmlHelpers
{
    public static MvcHtmlString IconLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, String iconName, object htmlAttributes = null)
    {
        var linkMarkup = htmlHelper.ActionLink(linkText, actionName, routeValues, htmlAttributes).ToHtmlString();
        var iconMarkup = String.Format("<span class=\"{0}\" aria-hidden=\"true\"></span>", iconName);
        return new MvcHtmlString(linkMarkup.Insert(linkMarkup.IndexOf(@"</a>"), iconMarkup));
    }
}
Ben Hale
  • 51
  • 1
  • 2
2

I should go with the approach of @Url.Action instead of @Html.ActionLink, se example code below:

<span>
<a href="@Url.Action("Create", new { @class = "btn btn-warning" })"><span class="glyphicon glyphicon-plus-sign"></span> Create New</a>
</span>
Webking
  • 1,822
  • 2
  • 20
  • 28
2

You can use simple extension:

private static readonly String SPAN_FORMAT = "<span class=\"{0}\" aria-hidden=\"true\"></span>";
private static readonly String A_END = "</a>";
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, String iconName, object htmlAttributes = null)
{
    var linkMarkup = htmlHelper.ActionLink(linkText, actionName, routeValues, htmlAttributes).ToHtmlString();
    if (!linkMarkup.EndsWith(A_END))
        throw new ArgumentException();

    var iconMarkup = String.Format(SPAN_FORMAT, iconName);
    return new MvcHtmlString(linkMarkup.Insert(linkMarkup.Length - A_END.Length, iconMarkup));
}

Usage:

Html.ActionLink(" ", "DeleteChart", new { Id = _.Id }, "glyphicon glyphicon-trash")
Andrey Burykin
  • 700
  • 11
  • 23
2

Try it!

@Html.ActionLink(" Cerrar sesión", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" , @class = "glyphicon glyphicon-log-in" })
Charlie
  • 349
  • 4
  • 8
0

Let's have a try on this. Let me know if it is working .Thanks

<style>
   span.super a
  {
      font: (your application font) !important;
  }

</style>

<span class="super">
    @Html.ActionLink("  Create New", "Create", null, new { @class = "btn btn-warning glyphicon glyphicon-plus-sign" })
</span>
Hatjhie
  • 1,366
  • 2
  • 13
  • 27
0

How about using Html.BeginForm with a FormMethod.Get / FormMethod.Post

@using (Html.BeginForm("Action", "Controller", new { Area = "" },
FormMethod.Get, htmlAttributes: new { title = "SomeTitle" }))
{   
   <button type="submit" class="btn-link" role="menuitem">
   <i class="glyphicon glyphicon-plus-sign"></i>Create New</button>
}
HostMyBus
  • 195
  • 2
  • 9
0

Try this. Worked for me.

<button class="btn btn-primary"><i class ="fa fa-plus">@Html.ActionLink(" ", "Create", "Home")</i></button>