我知道这是不对的,但为了说明问题,我想这样做:
<%= Html.Button("Action", "Controller") %>
我的目标是使一个HTML按钮,将调用我的MVC控制器的动作方法。
我知道这是不对的,但为了说明问题,我想这样做:
<%= Html.Button("Action", "Controller") %>
我的目标是使一个HTML按钮,将调用我的MVC控制器的动作方法。
当前回答
在所有的建议中,没有人使用razor语法(这也是bootstrap样式)。这将创建一个按钮,重定向到Account控制器中的Login视图:
<form>
<button class="btn btn-primary" asp-action="Login" asp-
controller="Account">@Localizer["Login"]</button>
</form>
其他回答
尽管使用onclick方法,你也可以像下面这样使用formaction:
<button type="submit" id="button1" name="button1" formaction='@Url.Action("Action", "Controller")'>Save</button>
根据上面的几个答案,你可以这样做:
<button onclick="location.href='@Url.Action("ActionName", "ControllerName")'" />
试试这个:
@Html.ActionLink("DisplayText", "Action", "Controller", route, attribute)
这应该对你有用。
这就是如何将表单提交给Razor中的特定控制器和动作方法。
<input type="submit" value="Upload" onclick="location.href='@Url.Action("ActionName", "ControllerName")'" />
<button type="button" onclick="location.href='@Url.Action("MyAction", "MyController")'" />
Type ="button"阻止页面提交,而是执行您的操作。