我知道这是不对的,但为了说明问题,我想这样做:

<%= Html.Button("Action", "Controller") %>

我的目标是使一个HTML按钮,将调用我的MVC控制器的动作方法。


当前回答

在所有的建议中,没有人使用razor语法(这也是bootstrap样式)。这将创建一个按钮,重定向到Account控制器中的Login视图:

    <form>
        <button class="btn btn-primary" asp-action="Login" asp- 
   controller="Account">@Localizer["Login"]</button>
    </form>

其他回答

尽管使用onclick方法,你也可以像下面这样使用formaction:

<button type="submit" id="button1" name="button1" formaction='@Url.Action("Action", "Controller")'>Save</button>
<button type="button" onclick="location.href='@Url.Action("MyAction", "MyController")'" />

Type ="button"阻止页面提交,而是执行您的操作。

最好用这个例子

<a href=“@Url.Action(”Register“,”Account“, new {id=Item.id })” class=“btn btn-primary btn-lg”>Register</a>

如果你得到一个错误为“unterminated string constant”,请使用以下razor语法:

<input type="button" onclick="@("location.href='"+ Url.Action("Index","Test")+ "'")" />

好的,你基本上需要将动作传递给按钮,并在点击发生时调用它,它不需要在from中,你可以使用HTML onclick on button在按钮被点击时触发它…

<button id="my-button" onclick="location.href='@Url.Action("YourActionName", "YourControllerName")'">Submit</button>