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?
问题:
回答1:
Is there some other way?
You could use a html <form>
:
@using (Html.BeginForm("someAction", "someController"))
{
<button type="submit">OK</button>
}
回答2:
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" })
回答3:
This seems very simple - am I missing something?
@Html.ActionLink("About", "About","Home", new { @class = "btn btn-success" })
回答4:
Use this:
@using (Html.BeginForm("yourAction", "yourController"))
{
<input type="submit" value="Some text"/>
}
回答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" })
回答6:
@using (Html.BeginForm("ActionName", "ControllerName")){
<input type="button" value="Submit" />
}
回答7:
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.
回答8:
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>
回答9:
use FORMACTION
<input type="submit" value="Delete" id="delete" formaction="@Url.Action("Delete", new { id = Model.Id })" />
回答10:
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>
回答11:
I know this is an old topic, but this helped me:
@Html.ActionLink("New member", "Create", null, new {@class = "btn btn-default"})
回答12:
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.