Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is how you should handle multiple submit buttons</p> <p>Below is a form with two submit buttons. Note that both these submit buttons have the same name i.e “submitButton”</p> <pre><code>@Html.BeginForm("MyAction", "MyController"); %&gt; &lt;input type="submit" name="submitButton" value="Button1" /&gt; &lt;input type="submit" name="submitButton" value="Button2" /&gt; } </code></pre> <p>Now over to the Controller, the Action takes in an input parameter called string stringButton and the rest is pretty self-explanatory.</p> <pre><code>public ActionResult MyAction(string submitButton) { switch(submitButton) { case "Button1": // do something here case "Button2": // do some other thing here default: // add some other behaviour here } ... } </code></pre> <p>Hope this helps you !</p> <p><strong>UPDATE :</strong> </p> <p>from your comments,</p> <blockquote> <p>Hi, I do know this workaround, my issue is the 2nd submit doesn't have to pass through the Controller: it has to call the javascript function in the View. The 1st post to a Controller in the right way, but THEN it runs the javascript function, too. </p> </blockquote> <p>Ok, instead of having two submit buttons, you can have one submit button and other as a normal button. Eg: -</p> <pre><code>@Html.BeginForm("MyAction", "MyController"); %&gt; &lt;input type="submit" name="submitButton" value="Button1" /&gt; &lt;input type="button" id="otherButton" value="Button2" /&gt; } </code></pre> <p>and then using some simple jquery, you can make a call to the javascript funcion <code>postData()</code>.</p> <pre><code>&lt;script type="text/javascript"&gt; $("#otherButton").bind("click", function () { console.log("do your stuff here"); postData(); }); &lt;/script&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload