Click here to Skip to main content
15,881,248 members
Articles / Web Development / ASP.NET
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.86/5 (34 votes)
22 May 2011CPOL 562.6K   41   36
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)


Written By
Technical Lead Infostretch Ahmedabad-Gujarat
India India
Aspiring for a challenging carrier wherein I can learn, grow, expand and share my existing knowledge in meaningful and coherent way.

sunaSaRa Imdadhusen


AWARDS:

  1. 2nd Best Mobile Article of January 2015
  2. 3rd Best Web Dev Article of May 2014
  3. 2nd Best Asp.Net article of MAY 2011
  4. 1st Best Asp.Net article of SEP 2010


Read More Articles...

Comments and Discussions

 
Questionhow can I pass Textbox box value on this button click Pin
sagar wasule5-Dec-12 23:13
sagar wasule5-Dec-12 23:13 
AnswerRe: how can I pass Textbox box value on this button click Pin
Sunasara Imdadhusen22-Apr-14 3:57
professionalSunasara Imdadhusen22-Apr-14 3:57 
GeneralRe: how can I pass Textbox box value on this button click Pin
sagar wasule24-Apr-14 2:04
sagar wasule24-Apr-14 2:04 
GeneralRe: how can I pass Textbox box value on this button click Pin
Sunasara Imdadhusen24-Apr-14 3:00
professionalSunasara Imdadhusen24-Apr-14 3:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.