views:

4806

answers:

7

Possible Duplicate:
ASP.NET MVC Ajax.ActionLink with Image

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

+12  A: 
<a href="<%=Url.RouteUrl(...)%>"><img src="..." alt="..." /></a>
Zack Peterson
+1 as this helped me. Thanks.
RichardOD
+2  A: 

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);
}
Chad Moran
This doesn't create an image action link though. Isn't that what Zack is asking for?
Nick DeVore
+5  A: 
<%= Html.ActionLink(Html.Image(imageUrl, imageAlt), actionName, controllerName) %>

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

AndreasN
No tag-soup. Nice.
Zack Peterson
I like this solution the best. Build on top of an existing block. Nice!
firefly
+2  A: 
<%= Html.RouteLink("PLACEHOLDER", ...).Replace("PLACEHOLDER", "<img src=""..."" alt=""..."" />")%>
Zack Peterson
This one requires a bunch of ugly string manipulation.
Zack Peterson
+9  A: 

here mine, its 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();

    }
MiniScalope
+1  A: 

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);
}
Oskar Austegard
+8  A: 

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();

    }
Gabriel Mongeon