how to make html.actionlink without text but with image inside

r.r picture r.r · Sep 14, 2010 · Viewed 16.1k times · Source

it is simple html:

<%--
<a href="?language=de">
    <img src="/Images/Company/de.png" alt="Webseite auf Deutsch" style="border: 0;" />
</a>
--%>

i would like to make from them html.actionlink:

 <%= Html.ActionLink("", "ChangeCulture", "Account", new { lang = "de", returnUrl = this.Request.RawUrl }, new { @style = "background-image: url(/Images/Company/de.png)", @class = "languageLink" }) %>

I would like to have my link without text. it doesn't work, null in a first parameter its not allowed. image is to short as normal. how can i use html.actionlink without text but with image??

Answer

Fenton picture Fenton · Sep 14, 2010

Here is a solution.

<a href="<%= Url.RouteUrl("MyRoute", new { id = Model.Id }) %>">
   <img src="myimage.png" alt="My Image" />.
</a>

Essentially, ActionLink is for text links and there isn't an equivalent for images, but you can use Url.RouteUrl to get the address for the link and put any HTML code you like inside of the anchor (W3C permitting of course).