Попытки передать вместо текста ссылки готовый тег '<img src='Icons/edit.png'/>' ни к чему хорошему не привели, потому как оно выводится через метод HttpUtility.HtmlEncode, который заменяет в тексте знаки '<' '>' на '<' и '>' соответственно.
Сделал с помощью Extension methods (методы-расширения) для класса HtmlHelper.
public static class Extensions
{
public static string ActionLinkImage(this HtmlHelper htmlHelper, string altImageText, string imagePath, string actionName)
{
return ActionLinkImage(htmlHelper, altImageText, imagePath, actionName, new RouteValueDictionary(), new RouteValueDictionary());
}
public static string ActionLinkImage(this HtmlHelper htmlHelper, string altImageText, string imagePath, string actionName, object routeValues)
{
return ActionLinkImage(htmlHelper, altImageText, imagePath, actionName, new RouteValueDictionary(routeValues), new RouteValueDictionary());
}
public static string ActionLinkImage(this HtmlHelper htmlHelper, string altImageText, string imagePath, string actionName, object routeValues, object htmlAttributes)
{
return ActionLinkImage(htmlHelper, altImageText, imagePath, actionName, new RouteValueDictionary(routeValues), new RouteValueDictionary(htmlAttributes));
}
public static string ActionLinkImage(this HtmlHelper htmlHelper, string altImageText, string imagePath, string actionName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
{
const string linkTextReplacement = "F14E9A0EB8C4";
string actionLink = System.Web.Mvc.Html.LinkExtensions.ActionLink(htmlHelper, linkTextReplacement, actionName, routeValues,htmlAttributes);
return actionLink.Replace(linkTextReplacement, String.Format(@"<img src='{0}' alt='{1}'/>", imagePath, altImageText));
}
}
А применяется оно так:
'<%= Html.ActionLinkImage("Edit", "Icons/edit.png", "Edit", new { id = item.EmployeeId }, new {@class = "ImageLink"})%'>
Результат:
'<A class=ImageLink href="about:/Employee/Edit/100"><IMG alt=Edit src="about:Icons/edit.png">'</A>
Комментариев нет:
Отправить комментарий