ailon's DevBlog: Development related stuff in my life

Html.ActionLink for images in ASP.NET MVC

12/27/2008 1:35:00 PM

As far as I understand there will be some methods in the future versions of ASP.NET MVC to get the same raw URL that is generated by Html.ActionLink helper method so you can wrap an image (or whatever) with a link, but in the current release (Beta) there's no such thing.

A quick google search revealed different ways people deal with this but first results weren't about the simplest workaround I just used and it works for me. So, I thought that someone might find this useful too.

The simple idea is just to place a marker as a link text and then just use simple string.Replace to place an image in place of the marker. Something like this:

<%= Html.ActionLink("__IMAGE_PLACEHOLDER__", "Products").Replace("__IMAGE_PLACEHOLDER__", "<img src=\"" + myImgUrl + "\" />")%>

Sure, this is not a very elegant solution but it works for me and it's simple.

Update: Oops. It appears that I wasn't aware of UrlHelper class and it's Action method which does just that (returns Action url). Thanks to James for pointing this out in the comments.

Tags: ,

blog comments powered by Disqus
Copyright © 2003 - 2014 Alan Mendelevich
Powered by BlogEngine.NET 2.5.0.6