【问题标题】:Build an ActionLink with an image inside (bootstrap)构建一个带有内部图像的 ActionLink(引导程序)
【发布时间】:2015-10-26 01:26:03
【问题描述】:

经过一些研究,我无法从我的问题中找到一个好的答案。

我有一个辅助方法来创建带有“+”号和标签的操作链接:

    public static HtmlString NewButton(this IHtmlHelper htmlHelper)
    {
        var htmlAttributes = new { @class = "btn btn-small btn-primary" };
        return htmlHelper.ActionLink("Insert", "Create", null, new RouteValueDictionary(), htmlAttributes);
    }

如何以我的最终 html 看起来像这样的方式包含“+”号?

<a href="/posts/new" class="btn btn-small btn-primary">
    <i class="ace-icon fa fa-plus"></i>
    Insert
</a>

在我的页面中,我像这样使用我的助手:

<div class="form-actions">
    @Html.NewButton()
</div>

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    我发现的唯一方法是将“Url”对象传递给我的方法,如下所示:

    public static HtmlString NewButton(this IHtmlHelper htmlHelper, IUrlHelper url)
    {
        var link = url.Action("Create");
        var el = string.Format("<a href = \"{0}\" class='btn btn-small btn-primary'><i class='ace-icon fa fa-plus'></i>Insert</a>", link);
        return new HtmlString(el);
    }
    

    在我看来,我是这样使用它的:

    @Html.NewButton(Url)
    

    我根本没有使用 ActionLink

    【讨论】:

    • 我想使用我的 png 图片
    【解决方案2】:

    你不能,就其本身而言。然而,没有什么说你必须使用Html.ActionLink。只需使用Url.Action,然后手动构建您的标签:

    <a href="@Url.Action("Insert", "Create")" class="btn btn-small btn-primary">
        <i class="ace-icon fa fa-plus"></i>
        Insert
    </a>
    

    【讨论】:

    • 我知道.. 但是使用助手,我可以使用以下方法构建我的页面:@Html.NewButton()
    • 因此,使用StringBuilder 和/或TagBuilder 更改您的助手以构建字符串,然后将其返回到包裹在MvcHtmlString 中:return new MvcHtmlString(builder.ToString());
    • 可以在不将“Url”变量传递给我的助手的情况下执行此操作吗?
    • 是的,您只需要使用来自HtmlHelperRequestContext 对象来实例化UrlHelper 的实例。见:stackoverflow.com/questions/1443647/generate-url-in-html-helper
    • 我想使用我的 png 图片
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-14
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多