简体   繁体   English

如何从同一视图执行3种不同的控制器方法

[英]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: 因此,最简单的想法是将您的inputsubmit )转换为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.

相关问题 如何在API控制器中有两个名称相同但参数不同的方法? - How to have two methods in API Controller with same name but different arguments? 从同一视图内的不同控制器调用方法 - Call methods from different controllers inside same view 如何从不同的控制器在视图中显示计数 - How to display count in view from a different controller 如何使用同一计时器在不同的时间间隔执行不同的方法 - How can I execute different methods on different intervals of time with the same timer 如何在同一个控制器中使用不同的参数调用不同的GET方法? - How can I call different GET Methods in the same Controller utilizing different parameters? 如何使用相同的控制器但不同的动作将数据从一个视图传递到另一个视图? ASP.Net 和 Microsoft Graph - How do you pass data from one view to another using the same controller but different actions? ASP.Net & Microsoft Graph 多个按钮调用相同的函数并执行不同的方法 - Multiple button calling same function and execute different methods 如何从使用另一控制器的一个控制器加载视图? - How to load view from one controller using a different one? 如何使模型验证来自其他控制器的局部视图? - how to make the model validate a partial view that comes from a different controller? 如何从视图MVC在不同的控制器方法之间传递模型 - How to pass model between different method of controller from view MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM