Friday, January 15, 2016

Multiple submit buttons in .NET MVC, Handle more than one button. Working tested 100%


  1. CASE (name attribute name should be same name as parameter in ActionResult method)
View (model) code--------------------------
@{
    ViewBag.Title = "Home";
    Layout = "~/Views/Shared/_LayoutAsh.cshtml";
}

<h2>Welcome to Home page with this master page</h2>

@using (Html.BeginForm("MultipleButton", "Home", FormMethod.Post))
{

@Html.TextBox("My")
<input type="submit" name="ASH" value="Save" />
<input type="submit" name="ASH" value="Update" />
<input type="submit" name="ASH" value="Delete" />
   
}
@ViewData["save"]

Controller (model) code--------------------------
public ActionResult ButtonForm()
        {           
            return View("Home");
        }
        public ActionResult MultipleButton(string ASH)
        {
            if (ASH == "Save")
            {
                ViewData["save"] = "save clicked";
            }
            if (ASH == "Update")
            {
                ViewData["save"] = "update clicked";
            }
            if (ASH == "Delete")
            {
                ViewData["save"] = "Delte clicked";
            }
            return View("Home");
        }
       

  1. CASE 2
View (model) code--------------------------

@{
    ViewBag.Title = "Home";
    Layout = "~/Views/Shared/_LayoutAsh.cshtml";
}

<h2>Welcome to Home page with this master page</h2>

@using (Html.BeginForm("MultipleButton", "Home", FormMethod.Post))
{

@Html.TextBox("My")

<input type="submit" name="button1" value="Save" />
<input type="submit" name="button2" value="Update" />
<input type="submit" name="button3" value="Delete" />
   
}
@ViewData["save"]


 Controller (model) code--------------------------


        public ActionResult ButtonForm()
        {           
            return View("Home");
        }

        public ActionResult MultipleButton()
        {
            if (Request.Form["button1"] == "Save")
            {
                ViewData["save"] = "save clicked";
            }
            if (Request.Form["button2"] == "Update")
            {
                ViewData["save"] = "update clicked";
            }
            if (Request.Form["button3"] == "Delete")
            {
                ViewData["save"] = "Delte clicked";
            }
            return View("Home");
        }

No comments:

Post a Comment