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

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

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


当前回答

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

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

其他回答

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

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

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

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

Razor语法如下:

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

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

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

这就是如何将表单提交给Razor中的特定控制器和动作方法。

 <input type="submit" value="Upload" onclick="location.href='@Url.Action("ActionName", "ControllerName")'" />