如何用@ Html.ActionLink中的glyphicon替换actionlink文本?

时间:2016-01-05 05:43:20

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

  

我想将X替换为glyphicon-trash

我使用X作为linktext.Which用于删除项目。如何替换为glyphicon。

这是我的代码

 @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');"})

3 个答案:

答案 0 :(得分:7)

你可以这样做。只需在name="date"参数中添加@class = "glyphicon glyphicon-trash",然后将htmlAttributes替换为X

space

答案 1 :(得分: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>

答案 2 :(得分:2)

这是我更容易重复使用的实现

帮助方法扩展类

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));    
        }
    }
}

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

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")  

使用bootstrap到彩色文本的基本链接类型按钮

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