如何整合以下代码
<a href="#" class="btn btn-primary"><i class="icon-plus"></i>Add</a>
到
@Html.ActionLink("Add", "xx", "Law", new { _lawfileid = -1, baseappid = ((ObjectModelLibrary.Law)ViewData["currentLaw"]).LawID }, new { @class = "btn btn-primary icon-plus" })
我无法将<i>
集成到Html.Actionlink。
如何将<i>
作为类集成到Html.Actionlink?
任何帮助将不胜感激。
感谢。
更新
HtmlHelperActionLinkExtensions类
public static IDisposable BeginActionLink<TModel>(this HtmlHelper<TModel> htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
{
return new DisposableActionLink(htmlHelper.ViewContext, () => BeginLink(htmlHelper, actionName, controllerName, routeValues, htmlAttributes), EndActionLink);
}
private static MvcHtmlString BeginLink(HtmlHelper htmlHelper, string actionName, string controllerName,
RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
{
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
var linkTag = new TagBuilder("a");
linkTag.Attributes.Add("href", urlHelper.Action(actionName, controllerName, routeValues));
linkTag.MergeAttributes(htmlAttributes);
return MvcHtmlString.Create(linkTag.ToString(TagRenderMode.StartTag));
}
private static MvcHtmlString EndActionLink()
{
return MvcHtmlString.Create("</a>");
}
DisposableActionLink类
public class DisposableActionLink : IDisposable
{
private readonly TextWriter _writer;
private Func<MvcHtmlString> End { get; set; }
public DisposableActionLink(ViewContext viewContext, Func<MvcHtmlString> begin, Func<MvcHtmlString> end)
{
_writer = viewContext.Writer;
End = end;
_writer.Write(begin().ToHtmlString());
}
public void Dispose()
{
_writer.Write(End().ToHtmlString());
}
}
查看:
@using Project.Models
@Html.BeginActionLink("Add", "Law", new RouteValueDictionary { { "_lawfileid", "-1" },
{ "baseappid", ((ObjectModelLibrary.Law)ViewData["currentLaw"]).LawID } }, new
Dictionary<string, object> { { "class", "btn btn-primary icon-plus" } })
{
<i class="icon-plus"></i>Add
}
结果图片
通常情况下,图标必须为+添加,但它会显示不同的内容。
照片
答案 0 :(得分:0)
@Html.ActionLink(new MvcHtmlString("<i class='icon-plus'>Add</i>").ToHtmlString(), "xx", "Law", new { _lawfileid = -1, baseappid = ((ObjectModelLibrary.Law)ViewData["currentLaw"]).LawID }, new { @class = "btn btn-primary"})
试试这个。
答案 1 :(得分:0)
处理问题的最简单方法是不使用Html.ActionLink。是的,它不支持嵌套标签,但你可以这样做:
<a href="@Url.Action("xx", "Law", new { _lawfileid = -1, baseappid = ((ObjectModelLibrary.Law)ViewData["currentLaw"]).LawID })" class="btn btn-primary icon-plus"><i class="icon-plus"></i>>Add</a>
<强> UPD:强> 还有另一种解决方法。虽然看起来有点hacky,但“hacky”的东西将被封装,所以没有人会知道:)
public class DisposableActionLink : IDisposable
{
private readonly TextWriter writer;
private Func<MvcHtmlString> End { get; set; }
public DisposableActionLink(ViewContext viewContext, Func<MvcHtmlString> begin, Func<MvcHtmlString> end)
{
writer = viewContext.Writer;
End = end;
writer.Write(begin().ToHtmlString());
}
public void Dispose()
{
writer.Write(End().ToHtmlString());
}
}
public static class HtmlHelperActionLinkExtensions
{
public static IDisposable BeginActionLink<TModel>(this HtmlHelper<TModel> htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
{
return new DisposableActionLink(htmlHelper.ViewContext, () => BeginLink(htmlHelper, actionName, controllerName, routeValues, htmlAttributes), EndActionLink);
}
private static MvcHtmlString BeginLink(HtmlHelper htmlHelper, string actionName, string controllerName,
RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
{
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
var linkTag = new TagBuilder("a");
linkTag.Attributes.Add("href", urlHelper.Action(actionName, controllerName, routeValues));
linkTag.MergeAttributes(htmlAttributes);
return MvcHtmlString.Create(linkTag.ToString(TagRenderMode.StartTag));
}
private static MvcHtmlString EndActionLink()
{
return MvcHtmlString.Create("</a>");
}
}
所以现在你可以使用它:
@using (Html.BeginActionLink("xx", "Law", new RouteValueDictionary{{"_lawfileid", "-1"}, {"baseappid", ((ObjectModelLibrary.Law)ViewData["currentLaw"]).LawID}}, new Dictionary<string, object>{{"class", "btn btn-primary icon-plus"}})
{
<i class="icon-plus"></i>@:Add
}