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

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

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


当前回答

试试这个:

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

这应该对你有用。

其他回答

最好用这个例子。

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

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

试试这个:

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

这应该对你有用。

<button type="button" onclick="location.href='@Url.Action("MyAction", "MyController")'" />

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

你可以使用Url。动作来指定生成控制器动作的url,所以你可以使用以下任何一个:

<form method="post" action="<%: Url.Action("About", "Home") %>">
   <input type="submit" value="Click me to go to /Home/About" />
</form>

or:

<form action="#">
  <input type="submit" onclick="parent.location='<%: Url.Action("About", "Home") %>';return false;" value="Click me to go to /Home/About" />
  <input type="submit" onclick="parent.location='<%: Url.Action("Register", "Account") %>';return false;" value="Click me to go to /Account/Register" />
</form>

根本不需要使用表单,除非你想要发布到动作。输入按钮(不是提交)就可以了。

  <input type="button"
         value="Go Somewhere Else"
         onclick="location.href='<%: Url.Action("Action", "Controller") %>'" />