ASP.NET Actionlink with glyphicon and text with different font

后端 未结 10 882
臣服心动
臣服心动 2020-12-04 15:12

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

With this code:


    @H         


        
相关标签:
10条回答
  • 2020-12-04 15:43

    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));
        }
    }
    
    0 讨论(0)
  • 2020-12-04 15:44

    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.

    0 讨论(0)
  • 2020-12-04 15:44

    Try this. Worked for me.

    <button class="btn btn-primary"><i class ="fa fa-plus">@Html.ActionLink(" ", "Create", "Home")</i></button>
    
    0 讨论(0)
  • 2020-12-04 15:47

    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>
    
    0 讨论(0)
  • 2020-12-04 15:50

    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>
    
    0 讨论(0)
  • 2020-12-04 15:51

    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>
    
    0 讨论(0)
提交回复
热议问题