asp.net-mvc 在@Html.ActionLink mvc asp.net 中插入 Glyphicons 引导程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23049256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:38:25  来源:igfitidea点击:

Insert Glyphicons bootstrap in @Html.ActionLink mvc asp.net

asp.net-mvc

提问by rysahara

Would enter Glyphicons Boostrap instead of "Edit" in the code below. Could you give me an example to do.

将在下面的代码中输入 Glyphicons Boostrap 而不是“编辑”。你能给我一个例子来做。

@Html.ActionLink("Edit", "Edit", new { id = Model.id_rod }) |

To bring up the image instead of writing.

调出图像而不是书写。

回答by Jeremy Cook

If using Bootstrap 3:

如果使用 Bootstrap 3:

<a href="@Url.Action("Edit", new { id = Model.id_rod })">
    <i class="glyphicon glyphicon-pencil"></i>
    <span class="sr-only">Edit</span>
</a>

Note the use of sr-onlywill allow users of screen readers and search engines to know what the link is for.

请注意,使用sr-only将使屏幕阅读器和搜索引擎的用户知道链接的用途。

回答by Mike

I made helper for easier re-use

我制作了助手以便更容易地重复使用

Helper Method Extension class

辅助方法扩展类

namespace Extensions {
    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 for the glyphicon                
                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));    
        }
    }
}

Make sure you include the namespace in the View so method is available

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

Example usage in Razor View (Leave area empty string if your not using areas)

Razor 视图中的示例用法(如果不使用区域,则将区域留空字符串)

@Html.FALink("Index", "ControllerNameHere", "Back to List", "th-list", "btn btn-info", "btn_back_to_list", new {area=""})
  • While I use Font Awesome you could easily replace fa with glyphicon class to use the bootstrap glyphicons.
  • If no controller or Action is passed in, it uses # as the link location so it will play nice with drop down interaction
  • 当我使用 Font Awesome 时,您可以轻松地将 fa 替换为 glyphicon 类以使用引导程序 glyphicon。
  • 如果没有传入控制器或动作,它将使用 # 作为链接位置,因此它可以很好地与下拉交互