Tuesday, November 15, 2011

Multi submit button on Single Form Asp.net Mvc 3.0

 

 

In Asp.net we have practice to use one submit button per one form and form method type in most case we have taken post and action method will firer. Now suppose you have scenario where you need to add more submit button in single form how it will work. Do you have any idea? Here role of ActionNameSelectorAttribute“ will come.

Here I explain in Mvc 3.0 how we will take multi Submit Button

Let’s Look My Very Simple View

 

 

   1:  @using (Html.BeginForm("Action", "Home")) { 
   2:    <input type="submit" name="create" value="Create" />
   3:    <input type="submit" name="read" value="Read" />
   4:    <input type="submit" name="update" value="Update" />
   5:    <input type="submit" name="delete" value="Delete" />
   6:   } 

 


I have taken Simple Class


   1:   public class HttpParamActionAttribute : ActionNameSelectorAttribute
   2:      {
   3:          public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
   4:          {
   5:              if (actionName.Equals(methodInfo.Name, StringComparison.InvariantCultureIgnoreCase))
   6:                  return true;
   7:   
   8:              if (!actionName.Equals("Action", StringComparison.InvariantCultureIgnoreCase))
   9:                  return false;
  10:   
  11:              var request = controllerContext.RequestContext.HttpContext.Request;
  12:              return request[methodInfo.Name] != null;
  13:          }
  14:      }




 


Now look My Controller


   1:    public class HomeController : Controller
   2:      {
   3:          //
   4:          // GET: /Home/
   5:   
   6:          public ActionResult Index()
   7:          {
   8:    
   9:              return View();
  10:          }       
  11:        
  12:          [HttpPost]
  13:          public ActionResult Index(UserInformation u)
  14:          {     
  15:              return View();
  16:          } 
  17:   
  18:          [HttpParamAction]
  19:          [HttpPost]
  20:          public ActionResult Create()
  21:          {
  22:              return View("");
  23:          }
  24:   
  25:          [HttpParamAction]
  26:          [HttpPost]
  27:          public ActionResult Read()
  28:          {
  29:              return View("");
  30:          }
  31:   
  32:          [HttpParamAction]
  33:          [HttpPost]
  34:          public ActionResult Update()
  35:          {
  36:              return View("");
  37:          }
  38:   
  39:          [HttpParamAction]
  40:          [HttpPost]
  41:          public ActionResult Delete()
  42:          {
  43:              return View("");
  44:          }
  45:   
  46:   
  47:      }



 


 


Now in Above Controller I have specified HttpParamAction in all method which will call as per my view. now when user press any button IsValidName  method will call and if Action and method name will not match it will create new post request and firer that method as per method name.

so using this way we can work with Multi submit button on Single Form Asp.net Mvc 3.0.

let me know if you need more help on this.   

2 comments:

  1. Hi Bhavik,
    Have you try to pass parameter like id with Actionresult ?
    i.e. public ActionResult Delete(int id)

    kindly check it out...

    ReplyDelete
  2. you should attribute this solution to the original author:

    http://blog.ashmind.com/2010/03/15/multiple-submit-buttons-with-asp-net-mvc-final-solution/

    ReplyDelete