ActionButton instead of ActionLink

ぃ、小莉子 提交于 2019-12-20 16:46:17

问题


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:


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>



回答8:


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.




回答9:


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>    



回答10:


use FORMACTION

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



回答11:


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

@Html.ActionLink("New member", "Create", null, new {@class = "btn btn-default"}) 



回答12:


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!!!




回答13:


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.



来源:https://stackoverflow.com/questions/11080601/actionbutton-instead-of-actionlink

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!