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

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

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


当前回答

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

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

其他回答

最好用这个例子。

使用ActionLink调用动作和控制器:

@Html.ActionLink("Submit", "Action", "Controller", route, new { @class = "btn btn-block"})

在控制器中实现动作时,使用

return View("Index");

or

return RedirectToAction("Index");

在索引。CSHTML(或生成动作的页面)页面已经定义。否则,您可能会遇到“视图或其主未找到…”错误。

来源:https://blogs.msdn.microsoft.com/aspnetue/2010/09/17/best-practices-for-asp-net-mvc/

如果你在主页(“/ home /Index”),你想调用管理控制器的索引动作,下面将为你工作。

<li><a href="/Admin/Index">Admin</a></li>

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

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

HTML <button>元素只能回发到包含它的表单。

因此,您需要创建一个post到动作的表单,然后在表单中放入一个<button>或<input type="submit" />。