Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
how to use multiple submit button in mvc with c#.
Posted

Try this. You can take the value of button by setting the name attribute as same to all the submit button
C#
@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"/>
}


C#
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
 
Share this answer
 
v2
1) Dirty one

XML
<input type="submit" id="Submit1" name="btnSubmit", value="action:First" />
<input type="submit" id="Submit2" name="btnSubmit", value="action:Second" />


SQL
[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:
C#
    [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
C#
[HttpPost]
 [MultipleButton(Name = "action", Argument = "First")]
 public ActionResult First(MyFormModel model) { ... }

 [HttpPost]
 [MultipleButton(Name = "action", Argument = "Second")]
 public ActionResult Second(MyFormModel model) { ... }
 
Share this answer
 
Comments
Abhinav S 1-Aug-13 1:58am    
If you press enter on the page, which one fires?
Vyacheslav Voronenko 1-Aug-13 2:00am    
The default submit button. It's up to you which button should be default.
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");
}
}
}

<input type="submit" value="button_A" name="btn"/>
<input type="submit" value="button_B" name="btn"/>
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900