Wednesday, November 09, 2011

MVC3 :: Multiple Submit button

You may need a form with more than one submit button.
Here you have the buttons at the Razor markup


 <input type="submit" value="Back" name="actionType" class="btn medium secondary" />
 <input type="submit" value="Save draft" name="actionType" class="btn medium secondary" />
 <input type="submit" value="Publish" name="actionType" class="btn medium primary" />



You have one action to submit the form to which name is "Edit" for example.
The solution is as you noticed above I've added name="actionType" for all submit buttons.
Now in the Edit action you have to add one extra parameter for actionType as below:

 public ActionResult Edit(string actionType)
{
    switch(actionType)
     {
               // add ur cases here
      }
}


2 comments:

  1. Hi, thanks for this.

    I am trying to use this, but am having a problem with some javascript that validates the form before posting.

    Do you know a way to get the clicked value or actionType in Javascript before the form is posted?

    Greg

    ReplyDelete
  2. Hi Greg,
    You can try RequiredIF attribute from the library foolproof
    http://foolproof.codeplex.com/

    Have a look

    Thanks

    Roshdy

    ReplyDelete