Is there an ASP.NET MVC HtmlHelper for image links?
Asked Answered
L

8

14

The Html.RouteLink() HtmlHelper works great for text links. But what's the best way to link an image?

Lael answered 23/3, 2009 at 21:33 Comment(0)
T
24

Here is mine, it`s the core function make some overloads

public static string ImageLink(this HtmlHelper htmlHelper, string imgSrc, string alt, string actionName, string controllerName, object routeValues, object htmlAttributes, object imgHtmlAttributes)
{
    UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
    string imgtag = htmlHelper.Image(imgSrc, alt,imgHtmlAttributes);
    string url = urlHelper.Action(actionName, controllerName, routeValues);

    TagBuilder imglink = new TagBuilder("a");
    imglink.MergeAttribute("href", url);
    imglink.InnerHtml =imgtag;
    imglink.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);

    return imglink.ToString();
}
Tut answered 4/8, 2009 at 12:41 Comment(3)
Shouldn't this be HtmlString instead of string? with return new HtmlString(imglink.ToString()); ?Brawny
Just to point out that htmlHelper.Image() is no longer in MVC4.Lactobacillus
cant declare htmlHelper.Image()Pawpaw
L
34
<a href="<%=Url.RouteUrl(...)%>"><img src="..." alt="..." /></a>
Lael answered 23/3, 2009 at 21:34 Comment(1)
+1 as this is the fastest method i used out of so many answered here. I just specified the action and controller in this method and it worked fine.Briefs
T
24

Here is mine, it`s the core function make some overloads

public static string ImageLink(this HtmlHelper htmlHelper, string imgSrc, string alt, string actionName, string controllerName, object routeValues, object htmlAttributes, object imgHtmlAttributes)
{
    UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
    string imgtag = htmlHelper.Image(imgSrc, alt,imgHtmlAttributes);
    string url = urlHelper.Action(actionName, controllerName, routeValues);

    TagBuilder imglink = new TagBuilder("a");
    imglink.MergeAttribute("href", url);
    imglink.InnerHtml =imgtag;
    imglink.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);

    return imglink.ToString();
}
Tut answered 4/8, 2009 at 12:41 Comment(3)
Shouldn't this be HtmlString instead of string? with return new HtmlString(imglink.ToString()); ?Brawny
Just to point out that htmlHelper.Image() is no longer in MVC4.Lactobacillus
cant declare htmlHelper.Image()Pawpaw
I
15

This is an updated version that I have from MiniScalope answer above. I'm using VS2010 and ASP.Net MVC 2 Preview

        public static string ImageLink(this HtmlHelper htmlHelper, string imgSrc, string alt, string actionName, string controllerName, object routeValues, object htmlAttributes, object imgHtmlAttributes)
    {
        UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
        TagBuilder imgTag = new TagBuilder("img");
        imgTag.MergeAttribute("src", imgSrc);
        imgTag.MergeAttributes((IDictionary<string, string>) imgHtmlAttributes,true);
        string url = urlHelper.Action(actionName, controllerName, routeValues);



        TagBuilder imglink = new TagBuilder("a");
        imglink.MergeAttribute("href", url);
        imglink.InnerHtml = imgTag.ToString();
        imglink.MergeAttributes((IDictionary<string, string>)htmlAttributes, true);

        return imglink.ToString();

    }
Institutionalize answered 25/1, 2010 at 3:48 Comment(0)
Q
9
<%= Html.ActionLink(Html.Image(imageUrl, imageAlt), actionName, controllerName) %>

could work, the image extension is from the futures assembly. Or make your own extention.

Quickie answered 23/3, 2009 at 21:40 Comment(1)
Doesn't seem to work. The Html.ActionLink() method appears to html encode the first arg so all the angle brackets etc are escaped.Emarie
R
7

Create your own helper extension.

public static string Image(this HtmlHelper helper, string src, string alt)
{
    TagBuilder tb = new TagBuilder("img");
    tb.Attributes.Add("src", helper.Encode(src));
    tb.Attributes.Add("alt", helper.Encode(alt));
    return tb.ToString(TagRenderMode.SelfClosing);
}
Relay answered 23/3, 2009 at 21:36 Comment(2)
This doesn't create an image action link though. Isn't that what Zack is asking for?Asuncionasunder
even if this doesn't directly answer the question I really like this method for creating an image tag helper extension. Thanks :)Emmery
B
5

I don't have enough SO swagger to add a comment, but this is a comment on MiniScalope's comment above:

UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;

I would suggest making this an HtmlHelper extension method in itself (and simplify it), for reuse:

private static UrlHelper Url(this HtmlHelper helper)
{ 
  return new UrlHelper(helper.ViewContext.RequestContext);
}
Byrann answered 22/10, 2009 at 15:43 Comment(0)
L
3
<%= Html.RouteLink("PLACEHOLDER", ...).Replace("PLACEHOLDER", "<img src=""..."" alt=""..."" />")%>
Lael answered 23/3, 2009 at 21:41 Comment(0)
S
2

this code has been tested on mvc4...

    public static MvcHtmlString ImageLink(this HtmlHelper htmlHelper, string imgSrc, string alt, string actionName, string controllerName, object routeValues, object htmlAttributes, object imgHtmlAttributes)
    {
        UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
        var imgTag = new TagBuilder("img");
        imgTag.MergeAttribute("src", imgSrc);
        imgTag.MergeAttributes((IDictionary<string, string>)imgHtmlAttributes, true);
        string url = urlHelper.Action(actionName, controllerName, routeValues);



        var imglink = new TagBuilder("a");
        imglink.MergeAttribute("href", url);
        imglink.InnerHtml = imgTag.ToString();
        imglink.MergeAttributes((IDictionary<string, string>)htmlAttributes, true);

        return MvcHtmlString.Create(imglink.ToString());

    }
Skyler answered 24/6, 2013 at 23:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.