经过一番研究,我找不到一个好的答案。
我有一个辅助方法来创建一个带有"+"号和标签的操作链接:
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>
我发现的唯一方法是将"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
你本身不能。然而,没有人说你必须使用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>