我正在尝试为ActionLink
带有图像的助手制作扩展方法。
这是我的扩展方法:
public static class MyHelpers
{
public static string ActionLinkWithImage(this HtmlHelper html, string imgSrc, string actionName)
{
var urlHelper = new UrlHelper(html.ViewContext.RequestContext);
string imgUrl = urlHelper.Content(imgSrc);
TagBuilder imgTagBuilder = new TagBuilder("img");
imgTagBuilder.MergeAttribute("src", imgUrl);
string img = imgTagBuilder.ToString(TagRenderMode.SelfClosing);
string url = urlHelper.Action(actionName);
TagBuilder tagBuilder = new TagBuilder("a")
{
InnerHtml = img
};
tagBuilder.MergeAttribute("href", url);
return tagBuilder.ToString(TagRenderMode.Normal);
}
}
我试图像这样使用它:
@Html.ActionLinkWithImage("Images/del.png", "Delete", new { id = item.ItemID});
但是我的扩展方法没有“路由值”。我该如何实施?