调用 MVC 控制器和操作方法的 HTML 按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2503923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:33:07  来源:igfitidea点击:

HTML button calling an MVC Controller and Action method

htmlasp.net-mvc

提问by Aaron Salazar

I know this isn't right, but for the sake of illustration I'd like to do something like this:

我知道这是不对的,但为了说明起见,我想做这样的事情:

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

My goal is to make an HTML button that will call my MVC controller's action method.

我的目标是制作一个 HTML 按钮来调用我的 MVC 控制器的操作方法。

回答by Cheddar

No need to use a form at all unless you want to post to the action. An input button (not submit) will do the trick.

除非您想发布到操作,否则根本不需要使用表单。输入按钮(不是提交)可以解决问题。

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

回答by Babul Mirdha

Razor syntax is here:

Razor 语法在这里:

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

回答by Amir Chatrbahr

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

type="button"prevents page from submitting. instead it performs your action.

type="button"阻止页面提交。相反,它会执行您的操作。

回答by Sunny Okoro Awa

Try this:

尝试这个:

@Html.ActionLink("DisplayText", "Action", "Controller", route, attribute)

This should work for you.

这应该对你有用。

回答by Jon Galloway

You can use Url.Action to specify generate the url to a controller action, so you could use either of the following:

您可以使用 Url.Action 指定生成控制器操作的 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>

回答by Debendra Dash

This is how you can submit your form to a specific controller and action method in Razor.

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

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

回答by usefulBee

Building on couple of the above answers, you could do this:

基于以上几个答案,您可以这样做:

<button onclick="location.href='@Url.Action("ActionName", "ControllerName")'" />

回答by SLaks

The HTML <button>element can only postback to the form containing it.

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

Therefore, you need to make a form that POSTs to the action, then put a <button>or <input type="submit" />in the form.

因此,您需要制作一个用于发布动作的表单,然后在表单中放置一个<button><input type="submit" />

回答by Alex Siela

it's better use this example

最好使用这个例子

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

回答by Gaurav Joshi

In case if you are getting an error as "unterminated string constant", use the following razor syntax :

如果您收到“未终止的字符串常量”错误,请使用以下剃刀语法:

<input type="button" onclick="@("location.href='"+ Url.Action("Index","Test")+ "'")" />