Click here to Skip to main content
15,888,351 members
Articles / Web Development / ASP.NET

How to handle multiple Submit button in ASP.NET MVC?

Rate me:
Please Sign up or sign in to vote.
4.67/5 (8 votes)
19 May 2015CPOL3 min read 63.3K   7   7
In this blog we will discuss how to handle multiple submit button issues in ASP.NET MVC

How to handle multiple Submit button issues in ASP.NET MVC? 

Let us understand the above problem in more detail.

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

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

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”.”

There are three basic approaches to solve the above problem scenario.

HTML 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.

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

<form action="Action2" method=post>
<input type="submit" name="Submit2">
</form>

AJAX way: -

In case you are an AJAX lover this second option would excite you more. In the Ajax way we can create two different functions “Fun1” and “Fun1”, see the below code. These functions will make Ajax calls by using JQUERY or any other framework. Each of these functions is binded with the “Submit” button’s “OnClick” events. Each of these function make call to respective action names.

HTML
<Script language="javascript">
function Fun1()
{
$.post("/Action1",null,CallBack1);
}
function Fun2()
{
$.post("/Action2",null,CallBack2);
}
</Script>

<form action="/Action1" method=post>
<input type=submit name=sub1 />
</form>
<form action="/Action2" method=post>
<input type=submit name=sub2 />
</form>

Using “ActionNameSelectorAttribute”: -

This is a great and a clean option. The “ActionNameSelectorAttribute” is a simple attribute class where we can write decision making logic which will decide which action can be executed.

So the first thing is in HTML we need to put proper name’s to the submit buttons for identifying them on the server.

You can see we have put “Save” and “Delete” to the button names. Also you can notice in the action we have just put controller name “Customer” and not a particular action name. We expect the action name will be decide by “ActionNameSelectorAttribute”.

HTML
<form action="Customer" method=post>
<input type=submit value="Save" name="Save" />

<input type=submit value="Delete" name="Delete"/>
</form>

So when the submit button is clicked, it first hits the “ActionNameSelector” attribute and then depending on which submit is fired it invokes the appropriate action.

Image 1

So the first step is to create a class which inherits from “ActionNameSelectorAttribute” class. In this class we have created a simple property “Name”.

We also need to override the “IsValidName” function which returns true or flase. This function is where we write the logic whether an action has to be executed or not. So if this function returns true then the action is executed or else it is not.

HTML
public class SubmitButtonSelector : ActionNameSelectorAttribute
    {
        public string Name { get; set; }
        public override bool IsValidName(ControllerContext controllerContext, string actionName, System.Reflection.MethodInfo methodInfo)
        {
            // Try to find out if the name exists in the data sent from form
var value = controllerContext.Controller.ValueProvider.GetValue(Name);
            if (value != null)
            {
                return true;
            }
            return false;

        }
    }

The main heart of the above function is in the below code. The “ValueProvider” collection has all the data that has been posted from the form. So it first looks up the “Name” value and if its found in the HTTP request it returns true or else it returns false.

HTML
var value = controllerContext.Controller.ValueProvider.GetValue(Name);
if (value != null)
      {
 return true;
      }
      return false;

This attribute class can then decorated on the respective action and the respective “Name” value can be provided. So if the submit is hitting this action and if the name matches of the HTML submit button name it then executes the action further or else it does not.

HTML
public class CustomerController : Controller
{
        [SubmitButtonSelector(Name="Save")]
        public ActionResult Save()
        {
            return Content("Save Called");
        }
        [SubmitButtonSelector(Name = "Delete")]
        public ActionResult Delete()
        {
            return Content("Delete Called");
        }
}

Below is a nice facebook video which demonstrates all the above three methos in a demonstrative and practical manner.

Image 2

For further reading do watch the below interview preparation videos and step by step video series.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect https://www.questpond.com
India India

Comments and Discussions

 
QuestionUsing a dropdownlist as submit button Pin
[DK]KiloDunse14-Dec-16 9:08
[DK]KiloDunse14-Dec-16 9:08 
GeneralMy vote of 4 Pin
Umesh AP8-Sep-16 1:45
Umesh AP8-Sep-16 1:45 
Very Nice Article. Helps me a lot. Thanks for sharing.
QuestionNeed to add 1 more Attribute Pin
Member 235504524-Dec-15 0:53
Member 235504524-Dec-15 0:53 
AnswerRe: Need to add 1 more Attribute Pin
Mohammad Jalal Ahmadzai3-Sep-17 9:34
Mohammad Jalal Ahmadzai3-Sep-17 9:34 
GeneralMy vote of 4 Pin
Manas_Kumar28-Nov-15 8:47
professionalManas_Kumar28-Nov-15 8:47 
QuestionHelp please! Pin
Bally Singh1-Jun-15 12:21
Bally Singh1-Jun-15 12:21 
AnswerRe: Help please! Pin
Bally Singh2-Jun-15 11:14
Bally Singh2-Jun-15 11:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.