[英]How to execute 3 different controller methods from the same view
I have created a C# ASP.NET MVC
application. 我创建了一个
C# ASP.NET MVC
应用程序。 In the Index
view, i have added 3 buttons, when each button is clicked i want to execute 3 different functions from the Index
controller. 在
Index
视图中,我添加了3个按钮,当单击每个按钮时,我想从Index
控制器执行3个不同的功能。
Index View that resides in the Home folder 驻留在主文件夹中的索引视图
@using (Html.BeginForm()) {
<input type="submit" value="b1" />
<input type="submit" value="b2" />
<input type="submit" value="b3" />
}
Home Controller 家庭控制器
public ActionResult Button1Click()
{
return View();
}
public ActionResult Button3Click()
{
return View();
}
public ActionResult Button2Click()
{
return View();
}
When each button is clicked how can i write code to execute the correct controller method ? 单击每个按钮后,如何编写代码以执行正确的控制器方法?
If you are posting then you can put each button in a separate form
: 如果要发布,则可以将每个按钮以单独的
form
放置:
@using (Html.BeginForm("Button1Click","Index")) {
<input type="submit" value="b1" />
}
@using (Html.BeginForm("Button2Click","Index")) {
<input type="submit" value="b2" />
}
@using (Html.BeginForm("Button3Click","Index")) {
<input type="submit" value="b3" />
}
If there is no data to post, as shown in your method, and you still want to have all buttons in the same form
then you can do an ajax post (this does not make sense though but hey I'm basing it on the code you gave in your question), with this though you may want to change your buttons from a submit into a button ( input type="button"
). 如果没有数据要发布(如您的方法所示),并且您仍然希望所有按钮都采用相同的
form
则可以执行ajax发布(虽然这没有意义,但是我将其基于代码您提出了问题),尽管如此,您可能希望将按钮从提交更改为按钮( input type="button"
)。
$("#b1").click(function(){
$.post('/index/button1click', function() {
});
});
$("#b2").click(function(){
$.post('/index/button2click', function() {
});
});
$("#b3").click(function(){
$.post('/index/button3click', function() {
});
});
If you want to do a GET instead of a post then just replace .post
with .get
. 如果要执行GET而不是发布,则只需将
.post
替换为.get
。
In MVC you need to remove the (Asp.Net) idea of linking button clicks to actions. 在MVC中,您需要删除(Asp.Net)将按钮单击链接到动作的想法。 ASP.Net is event driven MVC uses the classic HTTP REST approach.
ASP.Net是事件驱动的, MVC使用经典的HTTP REST方法。
So the buttons aren't actions, the buttons submit
actions. 因此,按钮不是动作,按钮会
submit
动作。 The action that is submitted is controlled by your form
. 提交的操作由您的
form
控制。 So your form POST
s data to the controller, using a HTTP post . 因此,您可以使用HTTP post将表单
POST
的数据发送到控制器。
Now it's not clear what your trying to achieve here. 现在不清楚您要在这里实现什么。 You appear to be returning different
view
s from each action
. 您似乎从每个
action
返回不同的view
。 So using the REST idea, you should be a GET
ing not a POST
ing (your getting HTML). 因此,使用REST想法,您应该是
GET
而不是POST
(您获取HTML)。 So the simplest idea is to turn your input
( submit
) into Anchor tag, ie a HTTP GET: 因此,最简单的想法是将您的
input
( submit
)转换为Anchor标记,即HTTP GET:
@Html.ActionLink("Button1Click")
etc. 等等
One easy way to execute different actions on different button within the same form is to distinguish button click by their name: 对同一表单中的不同按钮执行不同操作的一种简便方法是按按钮的名称来区分按钮单击:
Example code is: 示例代码是:
View: 视图:
@using (Html.BeginForm("MyMethod","Controller"))
{
<input type="submit" value="b1" name="b1" />
<input type="submit" value="b2" name="b2" />
<input type="submit" value="b3" name="b3" />
}
Controller: 控制器:
[HttpPost]
public ActionResult MyMethod(string b1, string b2, string b3)
{
if (b1 != null)
{
return Button1Click();
}
else if (b2 != null)
{
return Button2Click();
}
else
{
return Button3Click();
}
}
public ActionResult Button1Click()
{
return RedirectToAction("Index");
}
public ActionResult Button3Click()
{
return RedirectToAction("Index");
}
public ActionResult Button2Click()
{
return RedirectToAction("Index");
}
MVC doesn't work like Webforms where you have a ButtonClick event. MVC不能像WebForms那样具有ButtonClick事件。 Do you want to post any values to the controller?
是否要向控制器发布任何值?
If not, you can use a link that you can style like a button. 如果没有,则可以使用可以像按钮一样设置样式的链接。 Use the buildin Html extensions.
使用内置Html扩展。
//For links
@Html.ActionLink("Button1Text","Button1Click")
@Html.ActionLink("Button2Text","Button2Click")
@Html.ActionLink("Button3Text","Button3Click")
//If you need more styling options
<a href="@Html.Action("Button1Click")" class="btn">Button1</a>
<a href="@Html.Action("Button2Click")" class="btn">Button2</a>
<a href="@Html.Action("Button2Click")" class="btn">Button3</a>
That way you don't need any javascript or multiple forms in your view. 这样一来,您在视图中就不需要任何JavaScript或多种形式。 You'll have to add some styling in your CSS files.
您必须在CSS文件中添加一些样式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.