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: ,

Comments

1/11/2009 9:36:51 PM

James

What's wrong with using Url.Action ?

James United Kingdom

1/12/2009 9:41:28 AM

ailon

James: Oops. Nothing wrong. I just didn't know it existed Smile

ailon Lithuania

2/16/2009 7:18:23 PM

Avanta Meeting rooms

Been using the older versions of ASP.net. But like you highlighted above "The simple idea is just to place a marker as a link text and then just use simple string" . This should work !!

Avanta Meeting rooms United Kingdom

2/26/2009 6:06:45 AM

Ala

It appears that it just HTMLEncodes the string.

It's a bit of a hack, but perhaps try this?

Server.HTMLDecode(Html.ActionLink("<img src=blah>", "Products")

Ala

1/28/2010 1:33:19 AM

Marla

Thanks a lot your solution worked for me

Marla India

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Copyright © 2003 - 2010 Alan Mendelevich
Powered by BlogEngine.NET 1.6.1.0