ActionButton instead of ActionLink

2019-03-17 11:13发布

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?

12条回答
干净又极端
2楼-- · 2019-03-17 11:22

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.

查看更多
Summer. ? 凉城
3楼-- · 2019-03-17 11:26

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" })
查看更多
Ridiculous、
4楼-- · 2019-03-17 11:26

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>    
查看更多
SAY GOODBYE
5楼-- · 2019-03-17 11:26

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>
查看更多
Evening l夕情丶
6楼-- · 2019-03-17 11:27

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.

查看更多
虎瘦雄心在
7楼-- · 2019-03-17 11:28

use FORMACTION

<input type="submit" value="Delete" id="delete" formaction="@Url.Action("Delete", new { id = Model.Id })" />
查看更多
登录 后发表回答