Multiple Button in MVC 4

Many developers now have experience on ASP.NET Web development but when microsoft introduce the new solution for web development(ASP.NET Model View Controller - MVC framework) back at April 2009, not all developers like it. Because ASP.NET is just drag and drop, and easy to use different with ASP.NET MVC.

Recently i have develop one web application using MVC framework and this is my first experience using it i never done it before...

Just want to share some information for newbie like me to have a multiple button in one form. Sound like very easy but it is very popular in google search keyword "Multiple Button MVC" ..

ok here the solution i figure out :

Solution One

The View(cshtml)

    @using (Html.BeginForm("ActionTaken", "TestController"))
      {
           <button name="button" value="ActionOne" class="button" style="width: 200px;">
              test1</button>
           <button name="button" class="button" style="width: 160px;" value="ActionTwo">
             test2</button>       
       }


The Controller

          [AcceptVerbs(HttpVerbs.Post)]
          public ActionResult ActionTaken(string butt)
          {
             string ButtCommand= butt;
             switch (ButtCommand)
             {
                case "ActionOne":
                  //do stuff here
                case "ActionTwo":
                 //do stuff here
                default:
                        return View();
             }
          }


Solution Two

Sometimes when you use solution one and you click on the button, there is no event fire on the browser even the postback action not happen.(mostly in IE6) .. Here i figure out the second solution how to handle that thing.

change on the view part like this :

The View

    @using (Html.BeginForm("ActionTaken", "TestController"))
    {
       <input type="submit" class="button" style="width: 130px;" value="test1" />
       <input type="button" class="button" style="width: 150px;" value="test2"
             onclick=" onclick="$('#ResetForm').submit();" />
    }
    @using (Html.BeginForm("ResetButton", "TestController", FormMethod.Post, new { id = "ResetForm" }))
    {
    }

The Controller

   public ActionResult ResetButton()
   {
       //do stuff here
   }
  public ActionResult ActionTaken()
   {
       //do stuff here
   }



The second button will call form with id #ResetButton and postback to server..


By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

Popular posts from this blog

Example to disable save as certain file type in SSRS Report Viewer

How to create DataGrid or GridView in JSP - Servlet

Control Webpart Visible/Enable using macro in Kentico