[英]C# extention method for helper with route value
I'm trying to make an extension method for the ActionLink
helper with an image. 我正在尝试为带有图像的ActionLink
帮助器扩展方法。
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: 像这样:
public static string ActionLinkWithImage(this HtmlHelper html, string imgSrc, string actionName, object routeValues)
{
//Your code ...
string url = urlHelper.Action(actionName, routeValues);
}
Add an object parameter to your method 将对象参数添加到您的方法
, object routeData)
and pass this to the UrlHelper 并将其传递给UrlHelper
new UrlHelper(new RequestContext(html.ViewContext.HttpContext, routeData))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.