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

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

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


当前回答

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

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

其他回答

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

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

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

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

最好用这个例子

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

举个例子:

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

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

return View("Index");

or

return RedirectToAction("Index");

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

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