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控制器的操作方法。
16 个解决方案
#1
205
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") %>'" />
#2
180
Razor syntax is here:
剃须刀的语法是:
<input type="button" value="Create" onclick="location.href='@Url.Action("Create", "User")'" />
#3
43
<button type="button" onclick="location.href='@Url.Action("MyAction", "MyController")'" />
type="button" prevents page from submitting. instead it performs your action.
type="button"阻止页面提交。相反,它会执行你的动作。
#4
14
You can use Url.Action to specify generate the url to a controller action, so you could use either of the following:
您可以使用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>
#5
11
Try this:
试试这个:
@Html.ActionLink("DisplayText", "Action", "Controller", route, attribute)
This should work for you.
这应该对你有用。
#6
7
Building on couple of the above answers, you could do this:
基于以上几个答案,你可以这样做:
<button onclick="location.href='@Url.Action("ActionName", "ControllerName")'" />
#7
7
This is how you can subbmit your form to a specific controller and action method in Razor.
这就是如何将您的表单subbmit用于Razor中的特定控制器和操作方法。
<input type="submit" value="Upload" onclick="location.href='@Url.Action("ActionName", "ControllerName")'" />
#8
5
The HTML <button>
element can only postback to the form containing it.
HTML
Therefore, you need to make a form that POSTs to the action, then put a <button>
or <input type="submit" />
in the form.
因此,您需要将一个表单发送到操作,然后将 <按钮> 或 放入表单中。
#9
5
In case if you are getting an error as "unterminated string constant", use the following razor syntax :
如果您的错误是“未终止的字符串常量”,请使用以下razor语法:
<input type="button" onclick="@("location.href='"+ Url.Action("Index","Test")+ "'")" />
#10
4
When you implement the action in the controller, use
当您在控制器中执行操作时,请使用。
return View("Index");
or
或
return RedirectToAction("Index");
where Index.cshtml (or the page that generates the action) page is already defined. Otherwise you are likely encountering "the view or its master was not found..." error.
在索引。cshtml(或生成操作的页面)已经被定义。否则,您可能遇到“视图或它的主人没有发现…”错误。
Source: https://blogs.msdn.microsoft.com/aspnetue/2010/09/17/best-practices-for-asp-net-mvc/
来源:https://blogs.msdn.microsoft.com/aspnetue/2010/09/17/best-practices-for-asp-net-mvc/
#11
2
So, I'm using Razor but this will work using either. I'm basically wrapping a button in a link.
所以,我用的是剃刀,但这也可以用。我基本上是在链接一个按钮。
<a href="Controller/ActionMethod">
<input type="button" value="Click Me" />
</a>
#12
2
Despite onclick Method you can also use formaction as follows:
尽管onclick方法,您也可以使用formaction如下:
<button type="submit" id="button1" name="button1" formaction='@Url.Action("Action", "Controller")'>Save</button>
#13
1
it's better use this example
最好使用这个例子。
<a href="@Url.Action("Register","Account", new {id=Item.id })"
class="btn btn-primary btn-lg">Register</a>
#14
0
If you are in home page ("/Home/Index") and you would like to call Index action of Admin controller, following would work for you.
如果您在主页(“/ home /Index”)中,您希望调用管理控制器的索引操作,下面将为您工作。
<li><a href="/Admin/Index">Admin</a></li>
#15
0
OK, you basically need to pass the action to the button and call it when click happens, it doesn't need to be inside a from, you can use HTML onclick
on button to trigger it when the button get clicked...
好的,你需要把动作传递给按钮,当点击发生时调用它,它不需要在a里面,你可以使用HTML onclick on按钮触发它,当按钮被点击…
<button id="my-button" onclick="location.href='@Url.Action("YourActionName", "YourControllerName")'">Submit</button>
#16
0
it's better use this example.
最好使用这个例子。
Call action and controller using a ActionLink
:
使用ActionLink调用操作和控制器:
@Html.ActionLink("Submit", "Action", "Controller", route, new { @class = "btn btn-block"})
#1
205
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") %>'" />
#2
180
Razor syntax is here:
剃须刀的语法是:
<input type="button" value="Create" onclick="location.href='@Url.Action("Create", "User")'" />
#3
43
<button type="button" onclick="location.href='@Url.Action("MyAction", "MyController")'" />
type="button" prevents page from submitting. instead it performs your action.
type="button"阻止页面提交。相反,它会执行你的动作。
#4
14
You can use Url.Action to specify generate the url to a controller action, so you could use either of the following:
您可以使用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>
#5
11
Try this:
试试这个:
@Html.ActionLink("DisplayText", "Action", "Controller", route, attribute)
This should work for you.
这应该对你有用。
#6
7
Building on couple of the above answers, you could do this:
基于以上几个答案,你可以这样做:
<button onclick="location.href='@Url.Action("ActionName", "ControllerName")'" />
#7
7
This is how you can subbmit your form to a specific controller and action method in Razor.
这就是如何将您的表单subbmit用于Razor中的特定控制器和操作方法。
<input type="submit" value="Upload" onclick="location.href='@Url.Action("ActionName", "ControllerName")'" />
#8
5
The HTML <button>
element can only postback to the form containing it.
HTML
Therefore, you need to make a form that POSTs to the action, then put a <button>
or <input type="submit" />
in the form.
因此,您需要将一个表单发送到操作,然后将 <按钮> 或 放入表单中。
#9
5
In case if you are getting an error as "unterminated string constant", use the following razor syntax :
如果您的错误是“未终止的字符串常量”,请使用以下razor语法:
<input type="button" onclick="@("location.href='"+ Url.Action("Index","Test")+ "'")" />
#10
4
When you implement the action in the controller, use
当您在控制器中执行操作时,请使用。
return View("Index");
or
或
return RedirectToAction("Index");
where Index.cshtml (or the page that generates the action) page is already defined. Otherwise you are likely encountering "the view or its master was not found..." error.
在索引。cshtml(或生成操作的页面)已经被定义。否则,您可能遇到“视图或它的主人没有发现…”错误。
Source: https://blogs.msdn.microsoft.com/aspnetue/2010/09/17/best-practices-for-asp-net-mvc/
来源:https://blogs.msdn.microsoft.com/aspnetue/2010/09/17/best-practices-for-asp-net-mvc/
#11
2
So, I'm using Razor but this will work using either. I'm basically wrapping a button in a link.
所以,我用的是剃刀,但这也可以用。我基本上是在链接一个按钮。
<a href="Controller/ActionMethod">
<input type="button" value="Click Me" />
</a>
#12
2
Despite onclick Method you can also use formaction as follows:
尽管onclick方法,您也可以使用formaction如下:
<button type="submit" id="button1" name="button1" formaction='@Url.Action("Action", "Controller")'>Save</button>
#13
1
it's better use this example
最好使用这个例子。
<a href="@Url.Action("Register","Account", new {id=Item.id })"
class="btn btn-primary btn-lg">Register</a>
#14
0
If you are in home page ("/Home/Index") and you would like to call Index action of Admin controller, following would work for you.
如果您在主页(“/ home /Index”)中,您希望调用管理控制器的索引操作,下面将为您工作。
<li><a href="/Admin/Index">Admin</a></li>
#15
0
OK, you basically need to pass the action to the button and call it when click happens, it doesn't need to be inside a from, you can use HTML onclick
on button to trigger it when the button get clicked...
好的,你需要把动作传递给按钮,当点击发生时调用它,它不需要在a里面,你可以使用HTML onclick on按钮触发它,当按钮被点击…
<button id="my-button" onclick="location.href='@Url.Action("YourActionName", "YourControllerName")'">Submit</button>
#16
0
it's better use this example.
最好使用这个例子。
Call action and controller using a ActionLink
:
使用ActionLink调用操作和控制器:
@Html.ActionLink("Submit", "Action", "Controller", route, new { @class = "btn btn-block"})