Two submits on the same page

Asked

Viewed 446 times

2

I have a page with a list of checkbox and with two buttons: One is to View and the other to Send the report by e-mail. That is, the user will select the fields he needs to see in the report and he will select "View" or "Submit". I need to submit the form but for two Actions different and catch the FormCollection as parameter. Is there any way to submit the form to Actions distinct, according to the button the user presses?

1 answer

1


Translated from: How do you Handle Multiple Submit Buttons in ASP.NET MVC Framework?

Name each Ubmit button, and check that name in the action:

<% Html.BeginForm("MyAction", "MyController", FormMethod.Post); %>
<input type="submit" name="submitButton" value="Send" />
<input type="submit" name="submitButton" value="Cancel" />
<% Html.EndForm(); %>

Controller:

public class MyController : Controller {
    public ActionResult MyAction(string submitButton) {
        switch(submitButton) {
            case "Send":
                // delegate sending to another controller action
                return(Send());
            case "Cancel":
                // call another action to perform the cancellation
                return(Cancel());
            default:
                // If they've submitted the form without a submitButton, 
                // just return the view again.
                return(View());
        }
    }

    private ActionResult Cancel() {
        // process the cancellation request here.
        return(View("Cancelled"));
    }

    private ActionResult Send() {
        // perform the actual send operation here.
        return(View("SendConfirmed"));
    }

}
  • Hello @Rsinohara, is there any way to get by ID instead of Value?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.