Created
February 24, 2012 19:42
-
-
Save jirkapenzes/1903227 to your computer and use it in GitHub Desktop.
Image action link helper for ASP.NET MVC.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Routing; | |
namespace System.Web.Mvc | |
{ | |
public static class ImageActionLinkExtensions | |
{ | |
public static MvcHtmlString ImageActionLink(this HtmlHelper helper, string imagePath, string actionName, string controllerName, | |
object routeValues, object imageAttributes, object linkAttributes) | |
{ | |
var urlHelper = new UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection); | |
var aElementBuilder = new TagBuilder("a"); | |
aElementBuilder.MergeAttributes(new RouteValueDictionary(linkAttributes)); | |
aElementBuilder.MergeAttribute("href", urlHelper.Action(actionName, controllerName, routeValues)); | |
var imgElementBuilder = new TagBuilder("img"); | |
aElementBuilder.MergeAttributes(new RouteValueDictionary(imageAttributes)); | |
imgElementBuilder.MergeAttribute("src", imagePath); | |
aElementBuilder.InnerHtml = imgElementBuilder.ToString(TagRenderMode.SelfClosing); | |
return new MvcHtmlString(aElementBuilder.ToString(TagRenderMode.Normal)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment