我知道这是不对的,但为了说明问题,我想这样做:
<%= Html.Button("Action", "Controller") %>
我的目标是使一个HTML按钮,将调用我的MVC控制器的动作方法。
我知道这是不对的,但为了说明问题,我想这样做:
<%= Html.Button("Action", "Controller") %>
我的目标是使一个HTML按钮,将调用我的MVC控制器的动作方法。
当前回答
最好用这个例子
<a href=“@Url.Action(”Register“,”Account“, new {id=Item.id })” class=“btn btn-primary btn-lg”>Register</a>
其他回答
根本不需要使用表单,除非你想要发布到动作。输入按钮(不是提交)就可以了。
<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")'" />
最好用这个例子。
使用ActionLink调用动作和控制器:
@Html.ActionLink("Submit", "Action", "Controller", route, new { @class = "btn btn-block"})
在所有的建议中,没有人使用razor语法(这也是bootstrap样式)。这将创建一个按钮,重定向到Account控制器中的Login视图:
<form>
<button class="btn btn-primary" asp-action="Login" asp-
controller="Account">@Localizer["Login"]</button>
</form>
如果你在主页(“/ home /Index”),你想调用管理控制器的索引动作,下面将为你工作。
<li><a href="/Admin/Index">Admin</a></li>