Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image equivalent of ActionLink in ASP.NET MVC

In ASP.NET MVC is there an equivalent of the Html.ActionLink helper for Img tags?

I have a controller action that outputs a dynamically generated JPEG and I wanted to use the same Lambda expressions to link to it as I do HREFs using ActionLink.

Alternatively, a helper that just gives the URL to a route (again specified using Lambdas) would also be acceptable.

EDIT: I had originally specified that I was using Preview 5, however I see that a Beta has been released. So all-in-all the version number was an unneeded piece of info as I may be upgrading soon :-)

like image 334
Jason Whitehorn Avatar asked Oct 17 '08 00:10

Jason Whitehorn


People also ask

What is HTML ActionLink in MVC?

Html. ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.

What is difference between HTML ActionLink and URL action?

Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.


2 Answers

You can use the URL.Action method

<a href="<%= Url.Action("Create")  %>"><img src="../../Content/Images/add_48.png" /></a> 
like image 122
Robert MacLean Avatar answered Oct 16 '22 07:10

Robert MacLean


This question is older, and I just started recently with ASP.NET MVC when the RC was already out, but for those who find this question later like me this might be interesting:

At least in the RC you can use Url.Action() also with anonymous types, the result looks much nicer than the suggestions above, I guess:

<a href="<%= Url.RouteUrl("MyRoute", new { param1 = "bla", param2 = 5 }) %>">    put in <span>whatever</span> you want, also <img src="a.gif" alt="images" />. </a> 

There are many other overloads for RouteUrl as well, of course.

like image 37
realMarkusSchmidt Avatar answered Oct 16 '22 07:10

realMarkusSchmidt