Asp.net-mvc – Make an Html.ActionLink around an Image in ASP.NET MVC

asp.net-mvc

How can I do something similar to Html.ActionLink() except place the generated link around an Image instead of just spitting out the link?

Best Answer

Razor (View Engine):

<a href="@Url.Action("ActionName", "ControllerName")">
    <img src="@Url.Content("~/Content/img/imgname.jpg")" />
</a>

ASPX (View Engine):

<a href="<%= Url.Action("ActionName", "ControllerName") %>">
    <img src="<%= Url.Content("~/Content/img/imgname.jpg") %>" />
</a>

Obviously, if you do this more than once, write a helper for it. And fill in the other attributes of img/a. But this should give you the general idea.