Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

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

4.86/5 (34 votes)
22 May 2011CPOL 569.5K  
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
XML
@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


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

Using Image


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


Thanks,
Imdadhusen

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)