我知道这是不对的,但为了说明问题,我想这样做:
<%= Html.Button("Action", "Controller") %>
我的目标是使一个HTML按钮,将调用我的MVC控制器的动作方法。
我知道这是不对的,但为了说明问题,我想这样做:
<%= Html.Button("Action", "Controller") %>
我的目标是使一个HTML按钮,将调用我的MVC控制器的动作方法。
当前回答
在控制器中实现动作时,使用
return View("Index");
or
return RedirectToAction("Index");
在索引。CSHTML(或生成动作的页面)页面已经定义。否则,您可能会遇到“视图或其主未找到…”错误。
来源:https://blogs.msdn.microsoft.com/aspnetue/2010/09/17/best-practices-for-asp-net-mvc/
其他回答
试试这个:
@Html.ActionLink("DisplayText", "Action", "Controller", route, attribute)
这应该对你有用。
你可以使用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>
好的,你基本上需要将动作传递给按钮,并在点击发生时调用它,它不需要在from中,你可以使用HTML onclick on button在按钮被点击时触发它…
<button id="my-button" onclick="location.href='@Url.Action("YourActionName", "YourControllerName")'">Submit</button>
如果你得到一个错误为“unterminated string constant”,请使用以下razor语法:
<input type="button" onclick="@("location.href='"+ Url.Action("Index","Test")+ "'")" />
我用的是Razor,但这两者都可以。我基本上是在链接中包装一个按钮。
<a href="Controller/ActionMethod">
<input type="button" value="Click Me" />
</a>