How to implement multiple submit buttons in single view of ASP.Net MVC

We can do this task in many ways. But one simple approach to do this is using an “attribute-based solution”.

index

index save action

To do this we can do it like the following.
 
Step 1

Create a class, HttpParamActionAttribute.cs, in an ASP.Net MVC application.

  1. using System;  
  2. using System.Reflection;  
  3. using System.Web.Mvc;  
  4.    
  5. namespace MultipleButtonClick  
  6. {  
  7.     public class HttpParamActionAttribute : ActionNameSelectorAttribute  
  8.     {  
  9.         public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)  
  10.         {  
  11.             if (actionName.Equals(methodInfo.Name, StringComparison.InvariantCultureIgnoreCase))  
  12.                 return true;  
  13.    
  14.             var request = controllerContext.RequestContext.HttpContext.Request;  
  15.             return request[methodInfo.Name] != null;  
  16.         }  
  17.     }  
  18. }  
Step 2

Create a Home control and write some action like this:
  1. using System.Web.Mvc;  
  2.    
  3. namespace MultipleButtonClick.Controllers  
  4. {  
  5.     public class HomeController : Controller  
  6.     {  
  7.         //  
  8.         // GET: /Home/  
  9.    
  10.         public ActionResult Index()  
  11.         {  
  12.             ViewBag.msg = "I am from Index action.";  
  13.             return View();  
  14.         }  
  15.    
  16.         [HttpPost]  
  17.         [HttpParamAction]  
  18.         public ActionResult Save()  
  19.         {  
  20.             ViewBag.msg = "I am from Save action.";  
  21.             return View();  
  22.         }  
  23.    
  24.         [HttpPost]  
  25.         [HttpParamAction]  
  26.         public ActionResult Delete()  
  27.         {  
  28.             ViewBag.msg = "I am from Delete action.";  
  29.             return View();  
  30.         }  
  31.     }  
  32. }  
Step 3

Create a View, Index, and write the HTML code like this:

 

  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4.    
  5. <h2>Index</h2>  
  6.    
  7. @ViewBag.msg  
  8.    
  9. <br /> <br />  
  10. @using (@Html.BeginForm())  
  11. {  
  12.    
  13.     <input type="submit" name="Save" value="Save" />  
  14.    
  15.     <input type="submit" name="Delete" value="Delete" />  
  16. }  
Ebook Download
View all
Learn
View all