Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you handle multiple submit buttons in ASP.NET MVC Framework?

Is there some easy way to handle multiple submit buttons from the same form? For example:

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

Any idea how to do this in ASP.NET Framework Beta? All examples I've googled for have single buttons in them.

like image 702
Spoike Avatar asked Jan 14 '09 11:01

Spoike


People also ask

How do you handle multiple submit buttons in the same form?

Let's learn the steps of performing multiple actions with multiple buttons in a single HTML form: Create a form with method 'post' and set the value of the action attribute to a default URL where you want to send the form data. Create the input fields inside the as per your concern. Create a button with type submit.

Which function is used to handle multiple submit buttons?

Multiple buttons with different names To display a data entry textboxes EditorForModel() helper is used. You can very well use helpers such as TextBoxFor() and LabelFor() if you so wish. There are two submit buttons - one with name attribute set to save and the other with name of cancel.

Can you have multiple submit buttons in a form?

yes, multiple submit buttons can include in the html form. One simple example is given below.

Can we use multiple BeginForm in MVC?

Thanks for your help ! Multiple form tags should work fine in MVC unless they are nested.


2 Answers

Give your submit buttons a name, and then inspect the submitted value in your controller method:

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

posting to

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"));     }  } 

EDIT:

To extend this approach to work with localized sites, isolate your messages somewhere else (e.g. compiling a resource file to a strongly-typed resource class)

Then modify the code so it works like:

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

and your controller should look like this:

// Note that the localized resources aren't constants, so  // we can't use a switch statement.  if (submitButton == Resources.Messages.Send) {      // delegate sending to another controller action     return(Send());  } else if (submitButton == Resources.Messages.Cancel) {      // call another action to perform the cancellation      return(Cancel()); } 
like image 27
Dylan Beattie Avatar answered Oct 05 '22 14:10

Dylan Beattie


Here is a mostly clean attribute-based solution to the multiple submit button issue based heavily on the post and comments from Maarten Balliauw.

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] public class MultipleButtonAttribute : ActionNameSelectorAttribute {     public string Name { get; set; }     public string Argument { get; set; }      public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)     {         var isValidName = false;         var keyValue = string.Format("{0}:{1}", Name, Argument);         var value = controllerContext.Controller.ValueProvider.GetValue(keyValue);          if (value != null)         {             controllerContext.Controller.ControllerContext.RouteData.Values[Name] = Argument;             isValidName = true;         }          return isValidName;     } } 

razor:

<form action="" method="post">  <input type="submit" value="Save" name="action:Save" />  <input type="submit" value="Cancel" name="action:Cancel" /> </form> 

and controller:

[HttpPost] [MultipleButton(Name = "action", Argument = "Save")] public ActionResult Save(MessageModel mm) { ... }  [HttpPost] [MultipleButton(Name = "action", Argument = "Cancel")] public ActionResult Cancel(MessageModel mm) { ... } 

Update: Razor pages looks to provide the same functionality out of the box. For new development, it may be preferable.

like image 65
mkozicki Avatar answered Oct 05 '22 12:10

mkozicki