You must Sign In to post a response.
  • Category: ASP.Net MVC

    Multiple action for one view in MVC4 Architecture

    Hi,

    Can we have multiple action for one view i.e. add,update and search.
  • #767158
    Hi,

    do you want to use one view for update,add and view purpose?

    like viewdetail.cshtml is use for view,add and update

    this can be achieved using parameters

    this may help you

    http://www.codeproject.com/Articles/687061/Multiple-Models-in-a-View-in-ASP-NET-MVC-MVC

    thanks

  • #767161
    You can do it, depends on what version of ASP MVC you're using; with MVC 2, you can create an ascx control and use RenderAction
    in your view you'll put something like

    Html.RenderAction("Menu", "Navigation");

    and have a navigation controller with a Menu actionresult

    public class NavigationController : Controller
    {
    [ChildActionOnly]
    public ActionResult Menu()
    {
    Menu model;//your menu
    return PartialView("YourMenuAscxControlName", model);
    }
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #767170
    Hi,
    Yes you can, please refer below code:


    @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 });
    }


  • Sign In to post your comments