【问题标题】:How to replace actionlink text with a glyphicon in @Html.ActionLink?如何用@Html.ActionLink 中的字形替换actionlink 文本?
【发布时间】:2016-04-08 21:06:06
【问题描述】:

我想将X 替换为glyphicon-trash。

我使用X 作为链接文本。这适用于删除项目。如何用字形图标替换。

这是我的代码

 @Html.ActionLink(
                     "X",
                     "Delete",
                     "Grocery",
                     new { GroceryUsageID = item.GroceryUsageID, GroceryId = item.GroceryID },
                     new { @onclick = "return confirm('Are you sure you want to delete this Grocery');"})

【问题讨论】:

标签: asp.net asp.net-mvc twitter-bootstrap twitter-bootstrap-3 glyphicons


【解决方案1】:

你可以像下面那样做。只需在htmlAttributes 参数中添加@class = "glyphicon glyphicon-trash" 并将X 替换为space。

 @Html.ActionLink(
        " ",
        "Delete",
        "Grocery",
        new { GroceryUsageID = item.GroceryUsageID, GroceryId = item.GroceryID },
        new { @onclick = "return confirm('Are you sure you want to delete this Grocery');", 
              @class = "glyphicon glyphicon-trash" })

【讨论】:

    【解决方案2】:

    试试这个。复制自here ;)

    <a href="@Url.Action("Action", "Controller")" class="btn btn-warning">
        link text 
        <span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span>
    </a>
    

    【讨论】:

    • 我不认为 class="btn btn-warning" 是必要的,因为 glypicon-plus-sign 用于显示 + 符号。
    【解决方案3】:

    这是我的实现,便于重复使用

    辅助方法扩展类

    namespace Extensions {
    using System;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Mvc.Html;
        public static class HtmlExtensions {
            public static MvcHtmlString FALink(this HtmlHelper htmlHelper, string action, string controller, string link_text, string fa_class, string btn_css_classes = "", string button_id = "", object route_values = null) {
                    // declare the span                 
                    TagBuilder span = new TagBuilder("span");
                    span.AddCssClass($"fa fa-{fa_class}");
                    span.MergeAttribute("aria-hidden", "true");         
    
                    // declare the anchor tag
                    TagBuilder anchor = new TagBuilder("a");
                    // Add the href attribute to the <a> element
                    if (string.IsNullOrEmpty(controller) || string.IsNullOrEmpty(action))
                        anchor.MergeAttribute("href", "#");
                    else         
                        anchor.MergeAttribute("href", new UrlHelper(HttpContext.Current.Request.RequestContext).Action(action, controller, route_values));
    
                    // Add the <span> element and the text to the <a> element
                    anchor.InnerHtml = $"{span} {link_text}";
                    anchor.AddCssClass(btn_css_classes);
                    anchor.GenerateId(button_id);
                    // Create the helper
                    return MvcHtmlString.Create(anchor.ToString(TagRenderMode.Normal));    
            }
        }
    }
    

    确保在 View 中包含命名空间,以便方法可用

    Razor View 中的示例用法 您可以像使用任何股票 @Html 助手一样使用逗号分隔值来传递您想要的任何路由值

    使用所有参数:

    @Html.FALink("ActionNameHere", "ControllerNameHere", "Back to List", "th-list", "btn btn-info", "btn_back_to_list", new {area="AreaNameHere"})
    

    基本链接类型按钮:

    @Html.FALink("ActionNameHere", "ControllerNameHere", "Back to List, "th-list")  
    

    使用引导程序为文本着色的基本链接类型按钮

    @Html.FALink("ActionNameHere", "ControllerNameHere", "Back to List", "th-list, "btn-info")
    
    • 当我使用 Font Awesome 时,您可以轻松地将 fa 替换为 bootstrap glyphicon 类以使用 bootstrap glyphicons
    • 如果没有传入控制器或动作,它使用#作为链接位置,这样它就可以很好地与下拉交互

    【讨论】:

      猜你喜欢
      • 2014-08-16
      • 2012-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      • 1970-01-01
      • 2014-11-28
      • 2022-01-08
      相关资源
      最近更新 更多