Tuesday, October 14, 2014

How to handle Multiple Buttons in MVC

 View:
---------
@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { id = "submitForm" }))
{
@Html.TextBoxFor(m => m.Name, new { maxlength = 50 })

 <button type="submit" id="btnSave" name="command" value="Save">Save</button>
 <button type="submit" id="btnSubmit" name="command" value="Cancel"/>
}


Action:
----------
 public ActionResult Create(YourModel obj,string command)
{

//from here you can get the value of button you click
if(command="Save")
{

}
else if(command=="Cancel")
{
}
else
{
return RedirecToAction("Index");
}
return View();

}

No comments:

Post a Comment