Handling multiple submit buttons on the same form

Method 1 - Submit the form for each button

@using (Html.BeginForm("Department", "Edit", FormMethod.Post, new { id = "submitForm" }))
{
                 
     <div class="form-elements">
            @Html.Label("Department Code:*", new { id = "lblDepartmentCode" })
            @Html.TextBoxFor(model => model.DepartmentCode, new { id = "txtDepartmentCode" })
            @Html.ValidationMessageFor(model => model.DepartmentCode)
      </div>

      <div class="form-elements">
           @Html.Label("Department Name:*", new { id = "lblDepartmentName" })
           @Html.TextBoxFor(model => model.DepartmentName, new { id = "txtDepartmentName" })
            @Html.ValidationMessageFor(model => model.DepartmentName)
      </div>
   

<div id="divDepartmentbutton",class="masterButtons" >  
             
<input type="submit" id="btnDepartmentUpdate" class="masterSubmitButton" name="Command" value="Update"  onclick="return confirm('Are you sure to Update?');" />    

                   <input type="submit" id="btnDepartmentDelete" class="masterSubmitButton" name="Command" value="Delete"  onclick="return confirm('Are you sure to Delete?');"   />                    
                   <input type="submit" id="btnDepartmentcancel" class="masterSubmitButton" name="Command" value="Cancel(server side)"/>  

    </div>

}

 Action Method in Controller

  [HttpPost]
       public ActionResult Edit(M_Departments m_departments, string Command, int id = 0)
{
try
{
          if (ModelState.IsValid)
          {
             if (Command == "Update")
            {

               }
              if (Command == "Cancel")
             {

              }
            if (Command == "Delete")
            {

             }
}
catch (DataException ex)
{

}

return View();
}

Method 2 - Introducing Second From

@using (Html.BeginForm("Department", "Edit", FormMethod.Post, new { id = "submitForm" }))
{
                 
     <div class="form-elements">
            @Html.Label("Department Code:*", new { id = "lblDepartmentCode" })
            @Html.TextBoxFor(model => model.DepartmentCode, new { id = "txtDepartmentCode" })
            @Html.ValidationMessageFor(model => model.DepartmentCode)
      </div>

      <div class="form-elements">
           @Html.Label("Department Name:*", new { id = "lblDepartmentName" })
           @Html.TextBoxFor(model => model.DepartmentName, new { id = "txtDepartmentName" })
            @Html.ValidationMessageFor(model => model.DepartmentName)
      </div>
   

<div id="divDepartmentbutton",class="masterButtons" >  
             
<input type="submit" id="btnDepartmentUpdate" class="masterSubmitButton" name="Command" value="Update"  onclick="return confirm('Are you sure to Update?');" />    

                   <input type="submit" id="btnDepartmentDelete" class="masterSubmitButton" name="Command" value="Delete"  onclick="return confirm('Are you sure to Delete?');"   />                    
                   <input type="submit" id="btnDepartmentcancel" class="masterSubmitButton"  value="Cancel" name="Command"  onclick="$('#cancelForm').submit()"> 

  </div>

@using (Html.BeginForm("MultipleButtonCancel", "Home", FormMethod.Post, new { id = "cancelForm" })) { }


Action Method in Controller
 [HttpPost]
       public ActionResult MultipleButtonCancel()
{

}

Comments

Popular posts from this blog

How to retain the data annotations or class during modify a table in the database

Interview Questions

AJAX CONTROL