28

I want to create a button which when clicked calls an action. I have used Html.ActionLink to create a link which does exactly that but there is no Html.ActionButton. Is there some other way?

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304

13 Answers13

28

Here is an example using Bootstrap's CSS button and applying it against the link to make it look exactly like the button.

@Html.ActionLink("Select", "Select", new { id = patient.Id }, new { @class = "btn btn-primary", @style = "color:white" })
Flea
  • 11,176
  • 6
  • 72
  • 83
  • 3
    (Just nitpicking) Also add `role="button"` to the attributes to be 100% bootstrap reqs compliant – Alex Mar 26 '15 at 16:53
24

Is there some other way?

You could use a html <form>:

@using (Html.BeginForm("someAction", "someController"))
{
    <button type="submit">OK</button>
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
16

This seems very simple - am I missing something?

@Html.ActionLink("About", "About","Home", new { @class = "btn btn-success" })
Josef Van Zyl
  • 915
  • 3
  • 19
  • 43
Bicycle Dave
  • 484
  • 1
  • 7
  • 25
6

Use this:

@using (Html.BeginForm("yourAction", "yourController"))
{
    <input type="submit" value="Some text"/>
}
Jeow Li Huan
  • 3,758
  • 1
  • 34
  • 52
5
@using (Html.BeginForm("ActionName", "ControllerName")){
   <input type="button" value="Submit" />
}
Curtis
  • 101,612
  • 66
  • 270
  • 352
5
a[type="button"] {
    background-color: #d3dce0;
    border: 1px solid #787878;
    cursor: pointer;
    font-size: 1.2em;
    font-weight: 600;
    padding: 7px;
    margin-right: 8px;
    width: auto;
    text-decoration: none;
}

@Html.ActionLink("ActionName", "ControllerName",null, new {type = "button" })
user2539146
  • 51
  • 1
  • 2
5

Sometimes your button might have nested markup (e.g. for an icon)

This might be helpful to some people:

<button type="button" onclick="location.href='@Url.Action("Action", "Controller")'" class="btn btn-success">My button name <i class="glyphicon glyphicon-search"></i></button>

type=button is required to prevent page from submitting... Another possible solution (if you think type=button looks weird because the element is already a button) would be to call event.preventDefault() in the OnClick javascript handler.

<button onclick="event.preventDefault(); location.href='@Url.Action("Action", "Controller")'" class="btn btn-success">My button name <i class="glyphicon glyphicon-search"></i></button>
Greg R Taylor
  • 3,470
  • 1
  • 25
  • 19
2

This should answer your question.

Can I use an asp:Button like an Html.ActionLink?

Uses CSS to make the link look like a button.

Or use the button inside the form with the form controls moving the page along.

Community
  • 1
  • 1
Gabriel Baker
  • 1,209
  • 11
  • 21
2

You can write a link to controller and view this way / skip the whole razor notation (worked for me recently on my localhost!):

<a href="../Home(controller)/Index(view)"><button type="submit">OK</button></a>    
  • 1
    This breaks if you ever change your route config. The @Html.ActionLink approach is dynamic, and will work without depending on a certain route config – Alexander Jul 21 '16 at 20:25
2

use FORMACTION

<input type="submit" value="Delete" id="delete" formaction="@Url.Action("Delete", new { id = Model.Id })" />
Serge
  • 343
  • 5
  • 8
2

I know this is an old topic, but this helped me:

@Html.ActionLink("New member", "Create", null, new {@class = "btn btn-default"}) 
H35am
  • 768
  • 2
  • 12
  • 32
1

Make sure you use the correct overload or else you will be directed to the wrong controller.

you need to use a "null" before the new {} as shown below:

@Html.ActionLink("About", "Action Name","Controller Name",null, new { @class = "btn btn-success" })

Notice the null above. This is important!!!

Fabian Madurai
  • 317
  • 3
  • 4
0

For those of you trying to make an anchor tag look like a button in Bootstrap v3 (maybe v4, too), beware that there are some subtle styling differences regarding margin that bootstrap targets to the Input element and not the A element. This can be a little frustrating when trying to put several buttons next to each other.

There's a rather obvious reason why MVC doesn't have a ButtonLink helper: Think about it. Buttons do not link to anything. They run code. So what code would the helper render? I mean it seems simple that it could just render something like onclick="window.location=/controller/method..." but, is that really what you want? Is it what everyone always wants? Isn't that really just doing a GET? It gets kind of goofy.

So, really, if you want a button that opens a link the easiest way is to do what everyone else suggested and style an A tag to look like a button but then probably go into your stylesheet and do a very specific target of your button anchors to get the same styling as actual buttons. Or use an Input tag and force Razor (or whatever) to render the link inline with your JavaScript.

Newclique
  • 494
  • 3
  • 15