Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# MVC
how to use multiple submit button in mvc with c#.
Posted 31-Jul-13 19:39pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try this. You can take the value of button by setting the name attribute as same to all the submit button
@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { id = "submitForm" }))
{
@Html.TextBoxFor(m => m.Name, new { maxlength = 50 })
 
 <button type="submit" id="btnSave" name="command" value="Save">Save</button>
 <button type="submit" id="btnSubmit" name="command" value="Cancel"/>
}
 
public ActionResult Create(YourModel obj,string command)
{
 
//from here you can get the value of button you click
if(command="Save")
{
 
}
else if(command=="Cancel")
{
}
else
{
return RedirecToAction("Index");
}
return View();
 
}
If you want to point to different action try this link
http://blog.maartenballiauw.be/post/2009/11/26/Supporting-multiple-submit-buttons-on-an-ASPNET-MVC-view.aspx[^]
Hope this helps
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

1) Dirty one
 
<input type="submit" id="Submit1" name="btnSubmit", value="action:First" />
<input type="submit" id="Submit2" name="btnSubmit", value="action:Second" />
 
[HttpPost]
public ActionResult MyAction(string btnSubmit, MyFormModel model)
{
  switch (btnSubmit) {
    case "action:First":
      /* Your code */
      break;
    case "action:Second":
      /* Your code */
      break;
  }
 

2) Nicer - refactor code above to attribute
Like idea from
http://blog.maartenballiauw.be/post/2009/11/26/Supporting-multiple-submit-buttons-on-an-ASPNET-MVC-view.aspx[^]
 
Rephrasing code in article above:
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class MultipleButtonAttribute : ActionNameSelectorAttribute
{
    public string Name { get; set; }
    public string Argument { get; set; }
 
    public override bool IsValidName(ControllerContext controllerContext,
string actionName, MethodInfo methodInfo)
    {
        bool isValidName = false;
        string keyValue = string.Format("{0}:{1}", Name, Argument);
        var value = controllerContext.Controller.ValueProvider.GetValue(keyValue);
        if (value != null)
        {
            controllerContext.Controller.ControllerContext.RouteData.Values[Name] = Argument;
            isValidName = true;
        }
 
        return isValidName;
    }
}
 
and your code
   [HttpPost]
    [MultipleButton(Name = "action", Argument = "First")]
    public ActionResult First(MyFormModel model) { ... }
 
    [HttpPost]
    [MultipleButton(Name = "action", Argument = "Second")]
    public ActionResult Second(MyFormModel model) { ... }
  Permalink  
Comments
Abhinav S at 1-Aug-13 1:58am
   
If you press enter on the page, which one fires?
Vyacheslav Voronenko at 1-Aug-13 2:00am
   
The default submit button. It's up to you which button should be default.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

If you are working in asp.net with razor, and you want to control multiple submit button event.then this answer will guide you. Lets for example we have two button, one button will redirect us to "PageA.cshtml" and other will redirect us to "PageB.cshtml".
 
@{
if (IsPost)
{
if(Request["btn"].Equals("button_A"))
{
Response.Redirect("PageA.cshtml");
}
if(Request["btn"].Equals("button_B"))
{
Response.Redirect("PageB.cshtml");
}
}
}
<form method="post">
<input type="submit" value="button_A" name="btn"/>
<input type="submit" value="button_B" name="btn"/>
</form>
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 14 Feb 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100