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

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

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


当前回答

Razor语法如下:

<input type="button" value="Create" onclick="location.href='@Url.Action("Create", "User")'" />

其他回答

试试这个:

@Html.ActionLink("DisplayText", "Action", "Controller", route, attribute)

这应该对你有用。

举个例子:

<button name="nameButton" id="idButton" title="your title" class="btn btn-secondary" onclick="location.href='@Url.Action( "Index","Controller" new {  id = Item.id })';return false;">valueButton</button>

我用的是Razor,但这两者都可以。我基本上是在链接中包装一个按钮。

<a href="Controller/ActionMethod">
    <input type="button" value="Click Me" />
</a>

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

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

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

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