I’m trying to make an extension method for the ActionLink helper with an image.
This is my extension method:
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);
}
}
And im trying to use it like this:
@Html.ActionLinkWithImage("Images/del.png", "Delete", new { id = item.ItemID});
But my extension method does not have ‘route values’. How do i implement this?
Like this: