We can do this task in many ways. But one simple approach to do this is using an “attribute-based solution”.
To do this we can do it like the following.
Step 1
Create a class, HttpParamActionAttribute.cs, in an ASP.Net MVC application.
- using System;
- using System.Reflection;
- using System.Web.Mvc;
-
- namespace MultipleButtonClick
- {
- public class HttpParamActionAttribute : ActionNameSelectorAttribute
- {
- public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
- {
- if (actionName.Equals(methodInfo.Name, StringComparison.InvariantCultureIgnoreCase))
- return true;
-
- var request = controllerContext.RequestContext.HttpContext.Request;
- return request[methodInfo.Name] != null;
- }
- }
- }
Step 2
Create a Home control and write some action like this:
- using System.Web.Mvc;
-
- namespace MultipleButtonClick.Controllers
- {
- public class HomeController : Controller
- {
-
-
-
- public ActionResult Index()
- {
- ViewBag.msg = "I am from Index action.";
- return View();
- }
-
- [HttpPost]
- [HttpParamAction]
- public ActionResult Save()
- {
- ViewBag.msg = "I am from Save action.";
- return View();
- }
-
- [HttpPost]
- [HttpParamAction]
- public ActionResult Delete()
- {
- ViewBag.msg = "I am from Delete action.";
- return View();
- }
- }
- }
Step 3
Create a View, Index, and write the HTML code like this:
- @{
- ViewBag.Title = "Index";
- }
-
- <h2>Index</h2>
-
- @ViewBag.msg
-
- <br /> <br />
- @using (@Html.BeginForm())
- {
-
- <input type="submit" name="Save" value="Save" />
-
- <input type="submit" name="Delete" value="Delete" />
- }