65.9K
CodeProject is changing. Read more.
Home

Calling a MVC Controller and Action Method using HTML Button or Image

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.86/5 (32 votes)

May 19, 2011

CPOL
viewsIcon

569621

MVC 3

When creating a link to a controller action in ASP.NET MVC, using the generic ActionLink method is preferable, because it allows for strongly typed links that are refactoring friendly. Default: ActionLink
 @Html.ActionLink("Delete", "Delete", new { id = item.ID }) 
However, what if we want to have an image that links to an action? You might think that you could combine the ActionLink and Image and Button helpers like this:

Using Button

<input type="button" title="Delete" value="D" onclick="location.href='@Url.Action("Delete", "movies", new { id = item.ID })'" />

Using Image

<a href="@Url.Action("Delete", "movies", new { id = item.ID })" title="Edit">
<img src="../../Content/Images/Delete.png" />
</a>
Thanks, Imdadhusen