Multiple Submit Buttons in same form - MVC 4 Razor view

5693
Views
1
Answers

Hi I have a form in which I need to have multiple submit buttons. How do I know which button is clicked in the form.

edit | flag
Mad Maddy
Asked on: Feb 11, 2012 at 3:13AM

1 Answers

127
2
127

WE can do it in two ways..

Method 1:

Have two buttons with different name and identify them in the action using the name.
View

form  method="post" action="/Controller/action">
    input type
="button" name="submit1" id="button1" />
    input type
="button" name="submit2" id="button2" />  
/form>

Controller:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Action (FormCollection fc)
       
{
           
if (fc["submit1"] != null)
           
{
               
//do some action for submit1 click
           
}
             
if (fc["submit2"] != null)
           
{
               
//do some action for submit2 click
           
}
       
}

Method 2:

Have two different form in the same page and post them to their respective actions in the controller.
View

% Html.BeginForm("Action1", "Controller); %>
   input type="
button" name="submit" id="button1" />
% Html.EndForm(); %>

% Html.BeginForm("
Action2", "Controller"); %>
    input type="
button" name="submit" id="button2" />
% Html.EndForm(); %>

Controller

[HttpPost]
public ActionResult Action1(FormCollection fc)
{
 
// Code for Click of button1
}

[HttpPost]
public ActionResult Action2(FormCollection fc)
{
 
// Code for Click of button2
}
edit | flag
Gokul A
Answered on: Feb 11, 2012 at 3:30AM

Post your Answer

Search

Welcome to Ask Amoeba!
This is 100% free and interactive site for sharing professional Questions and Answers, Opensource projects, Interview questions.
Learners, beginners, Experts stop and share your knowledge and ideas!

Browse Categories

Browse Tags