Click here to Skip to main content
15,880,543 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

 
GeneralMy vote of 5 Pin
Savalia Manoj M7-Nov-12 1:13
Savalia Manoj M7-Nov-12 1:13 
GeneralRe: My vote of 5 Pin
Sunasara Imdadhusen22-Apr-14 3:56
professionalSunasara Imdadhusen22-Apr-14 3:56 
Questionmy 5! Pin
kimberly wind15-Jun-12 18:23
kimberly wind15-Jun-12 18:23 
AnswerRe: my 5! Pin
Sunasara Imdadhusen22-Apr-14 3:59
professionalSunasara Imdadhusen22-Apr-14 3:59 
QuestionPOST method in action link Pin
kimberly wind15-Jun-12 0:45
kimberly wind15-Jun-12 0:45 
AnswerRe: POST method in action link Pin
Sunasara Imdadhusen15-Jun-12 3:11
professionalSunasara Imdadhusen15-Jun-12 3:11 
GeneralRe: POST method in action link Pin
kimberly wind15-Jun-12 17:52
kimberly wind15-Jun-12 17:52 
GeneralRe: POST method in action link Pin
Sunasara Imdadhusen15-Jun-12 18:18
professionalSunasara Imdadhusen15-Jun-12 18:18 
AnswerRe: POST method in action link Pin
Sunasara Imdadhusen15-Jun-12 18:19
professionalSunasara Imdadhusen15-Jun-12 18:19 
It will set id of Link to value of item.id from model.

Thanks,
Imdadhusen

sunaSaRa Imdadhusen
+91 99095 44184

GeneralRe: POST method in action link Pin
kimberly wind15-Jun-12 18:22
kimberly wind15-Jun-12 18:22 
GeneralRe: POST method in action link Pin
Sunasara Imdadhusen15-Jun-12 22:47
professionalSunasara Imdadhusen15-Jun-12 22:47 
GeneralRe: POST method in action link Pin
Vitaliy Markitanov29-Jul-15 10:27
Vitaliy Markitanov29-Jul-15 10:27 
GeneralMy vote of 4 Pin
Manoj Kumar Choubey1-Jun-12 20:27
professionalManoj Kumar Choubey1-Jun-12 20:27 
GeneralRe: My vote of 4 Pin
Sunasara Imdadhusen22-Apr-14 3:56
professionalSunasara Imdadhusen22-Apr-14 3:56 
GeneralThanks Nirmal for your vote and appreciation. Pin
Sunasara Imdadhusen1-Aug-11 17:56
professionalSunasara Imdadhusen1-Aug-11 17:56 
GeneralReason for my vote of 5 Easy to understand and very helpful Pin
NIRMAL UPADHYAY21-Jun-11 18:34
NIRMAL UPADHYAY21-Jun-11 18:34 
GeneralRe: Reason for my vote of 5Easy to understand and very helpful Pin
Sunasara Imdadhusen22-Apr-14 3:58
professionalSunasara Imdadhusen22-Apr-14 3:58 

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.