I have two buttons on my MVC form:
<input name="submit" type="submit" id="submit" value="Save" />
<input name="process" type="submit" id="process" value="Process" />
From my Controller action how do I know which one have been pressed?
I have two buttons on my MVC form:
<input name="submit" type="submit" id="submit" value="Save" />
<input name="process" type="submit" id="process" value="Process" />
From my Controller action how do I know which one have been pressed?
you can identify your button from there name tag like below, You need to check like this in you controller
Can you not find out using Request.Form Collection? If process is clicked the request.form["process"] will not be empty
This post is not going to answer to Coppermill, because he have been answered long time ago. My post will be helpful for who will seeking for solution like this. First of all , I have to say " WDuffy's solution is totally correct" and it works fine, but my solution (not actually mine) will be used in other elements and it makes the presentation layer more independent from controller (because your controller depend on "value" which is used for showing label of the button, this feature is important for other languages.).
Here is my solution, give them different names , Like:
`
And you must specify the names of buttons as arguments in the action like below:
when user submits the page using one of the buttons, only one of the arguments will have value. I guess this will be helpful for others.
Update
This answer is quite old and I actually reconsider my opinion . maybe above solution is good for situation which passing parameter to model's properties. don't bother yourselves and take best solution for your project.
Name both your submit buttons the same
Then in your controller get the value of submit. Only the button clicked will pass its value.
You can of course assess that value to perform different operations with a switch block.
this is a better answer, so we can have both text and value for a button:
http://weblogs.asp.net/dfindley/archive/2009/05/31/asp-net-mvc-multiple-buttons-in-the-same-form.aspx
and the controller:
in short its a SUBMIT button but you choose the name using the name attribute, its even more powerful because your not obligated to the name submit or button in the controller method parameters, you can call it as you like...