top button
Flag Notify
Site Registration

How can you handle multiple Submit buttons pointing to multiple actions in a single MVC view?

+4 votes
How can you handle multiple Submit buttons pointing to multiple actions in a single MVC view?
posted Mar 31, 2015 by Merry

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Let us elaborate on what the interviewer wants to ask because the above question is just a single liner and is not clear about what the interviewer wants.

Take a scenario where you have a view with two submit buttons as shown in the below code.

<form action="Action1" method=post>
<input type=”submit” name=”Submit1”/>
<input type=”submit” name=”Submit2”>

In the above code when the end user clicks on any of the submit buttons it will make a HTTP POST to “Action1”.

The question from the interviewer is:-

“What if we have want that on “Submit1” button click it should invoke “Action1” and on the “Submit2” button click it should invoke “Action2”.”

Now that we have understood the question let us answer the question in a detailed manner. There are two approaches to solve the above problem one is the normal HTML way and the other is the “Ajax” way.

In the HTML way we need to create two forms and place the “Submit” button inside each of the forms. And every form’s action will point to different / respective actions. You can see the below code the first form is posting to “Action1” and the second form will post to “Action2” depending on which “Submit” button is clicked.

<form action="Action1" method=post>
<input type=”submit” name=”Submit1”/>

In case the interviewer complains that the above approach is not AJAX this is where the second approach comes in. In the Ajax way we can create two different functions “Fun1” and “Fun1” , see the below code. These function will make Ajax calls by using JQUERY or any other framework. Each of these functions are binded with the “Submit” button’s “OnClick” events.

<Script language="javascript">
function Fun1()
function Fun2()
answer Feb 20, 2017 by Jdk
Similar Questions
+2 votes

Is it possible use two ( multiple) models with a single view? If yes How?