Multiple action for one view in MVC4 Architecture
Hi,Can we have multiple action for one view i.e. add,update and search.
Html.RenderAction("Menu", "Navigation");
public class NavigationController : Controller
{
[ChildActionOnly]
public ActionResult Menu()
{
Menu model;//your menu
return PartialView("YourMenuAscxControlName", model);
}
}
@Html.ActionLink("Insert", "PersonDetails", new { id = item.ID }) |
@Html.ActionLink("Update", "PersonDetails", new { id = item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ID })
public ActionResult PersonDetails(int? id)
{
ViewData["Operation"] = id;
ViewData["users"] = db.Persons.ToList();
Person p = db.Persons.Find(id);
return View(p);
}
[HttpPost]
[ActionName("PersonDetails")]
[OnAction(ButtonName = "Insert")]
public ActionResult Insert(Person p)
{
if (ModelState.IsValid)
{
db.Persons.Add(p);
db.SaveChanges();
}
return RedirectToAction("PersonDetails");
}
[HttpPost]
[ActionName("PersonDetails")]
[OnAction(ButtonName = "Update")]
public ActionResult Update(Person per)
{
if (ModelState.IsValid)
{
db.Entry(per).State = EntityState.Modified;
db.SaveChanges();
}
return RedirectToAction("PersonDetails", new { id = 0 });
}
public ActionResult Delete(int id)
{
Person p = db.Persons.Find(id);
db.Persons.Remove(p);
db.SaveChanges();
return RedirectToAction("PersonDetails", new { id = 0 });
}