views:

28

answers:

2

How do I implement CRUD access with actions using the same view?

class UserController : Controller
{
   [ActionName("User")]
   [HttpGet]
   public ActionResult GetUser() {/* ... */}

   [ActionName("User")]
   [HttpPost]
   public ActionResult PostUser() {/* ... */}
}

I would like both actions to use the same view.
Is there an attribute to specify what view to use?

+5  A: 

You can call View("") at the end of each method, e.g.:

public ActionResult GetUser(int id)
{
   User user; // Do work
   return View("DisplayUser", user);
}

public ActionResult PostUser(User user)
{
   // Do work
   return View("DisplayUser", user);
}
Matthew Abbott
+1  A: 
return View("ViewName");
Paul Creasey