Click here to Skip to main content
15,860,943 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 561.9K   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

 
Question<input> does not have an onclick attribute Pin
Dylan Chen26-Jan-16 5:10
Dylan Chen26-Jan-16 5:10 
Suggestionmvc sh*t Pin
SAGIRAJU SIVA NAGENDRA VARMA19-Oct-15 0:22
professionalSAGIRAJU SIVA NAGENDRA VARMA19-Oct-15 0:22 
QuestionI don't understand id=item.id Pin
scvn809-Jun-15 5:15
scvn809-Jun-15 5:15 
AnswerRe: I don't understand id=item.id Pin
Sunasara Imdadhusen7-Dec-15 18:28
professionalSunasara Imdadhusen7-Dec-15 18:28 
GeneralMy vote of 1 Pin
Member 111137402-Jun-15 23:04
Member 111137402-Jun-15 23:04 
GeneralMy vote of 5 Pin
Mahsa Hassankashi17-Aug-14 11:09
Mahsa Hassankashi17-Aug-14 11:09 
GeneralRe: My vote of 5 Pin
Sunasara Imdadhusen17-Aug-14 23:40
professionalSunasara Imdadhusen17-Aug-14 23:40 
QuestionGreat One Imdad Bhai Pin
NIRMAL UPADHYAY6-Mar-14 19:20
NIRMAL UPADHYAY6-Mar-14 19:20 
AnswerRe: Great One Imdad Bhai Pin
Sunasara Imdadhusen6-Mar-14 19:23
professionalSunasara Imdadhusen6-Mar-14 19:23 
QuestionNeat little bit of code Pin
Member 1015611214-Aug-13 1:03
Member 1015611214-Aug-13 1:03 
AnswerRe: Neat little bit of code Pin
Sunasara Imdadhusen22-Apr-14 3:55
professionalSunasara Imdadhusen22-Apr-14 3:55 
Generalthanks Pin
patelvipul_be14-Mar-13 13:49
patelvipul_be14-Mar-13 13:49 
GeneralRe: thanks Pin
Sunasara Imdadhusen22-Apr-14 3:58
professionalSunasara Imdadhusen22-Apr-14 3:58 
QuestionWell Done Pin
Member 925303813-Dec-12 10:21
Member 925303813-Dec-12 10:21 
AnswerRe: Well Done Pin
Sunasara Imdadhusen22-Apr-14 3:58
professionalSunasara Imdadhusen22-Apr-14 3:58 
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 
Create a viewModel if you haven't already that contains a property for JobID

C#
public class Model
{
 public string JobId {get; set;}
 public IEnumerable<MyCurrentModel> myCurrentModel {get; set;}
 //...any other properties you may need
}


Strongly type your view

C#
@model Fully.Qualified.Path.To.Model


Add a hidden field for JobId to the form

C#
using (@Html.BeginForm("myMethod", "Home", FormMethod.Post)){

//...

@Html.HiddenFor(m => m.JobId)
}

And accept the model as the parameter in your controller action:
C#
[HttpPost]
    public FileStreamResult myMethod(Model model)
    {
         sting str = model.JobId;

    }

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 

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.