简体   繁体   中英

How to execute 3 different controller methods from the same view

I have created a C# ASP.NET MVC application. 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 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 :

@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" ).

$("#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 .

In MVC you need to remove the (Asp.Net) idea of linking button clicks to actions. ASP.Net is event driven MVC uses the classic HTTP REST approach.

So the buttons aren't actions, the buttons submit actions. The action that is submitted is controlled by your form . So your form POST s data to the controller, using a HTTP post .

Now it's not clear what your trying to achieve here. You appear to be returning different view s from each action . So using the REST idea, you should be a GET ing not a POST ing (your getting HTML). So the simplest idea is to turn your input ( submit ) into Anchor tag, ie a 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. 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.

//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. You'll have to add some styling in your CSS files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM