views:

39

answers:

1

Problem:

  1. Calendar application built using ASP.NET MVC2
  2. From the main calendar page, the user can select an add appointment link that brings up a form to create a new appointment.
  3. After the appointment is created, we want to return to the main calendar page, potentially with an error or confirmation message.
  4. There are other pages within the application that also link to the create form. The form should always return to the page that the user was on when they chose to create an appointment.

The basic flow is originating page -> form -> originating page.

In the form controller we don't want to hard code the return action, but rather say something like

Create(AppointmentModel model)
{
  ...
  return RedirectToTheReferringAction(...)

Are their best practices for implementing this?

A: 

in this case, I would set this on any view that has the widget:

ViewData["controller"] = Request.RequestContext.RouteData.Values["controller"];
ViewData["action"] = Request.RequestContext.RouteData.Values["action"];

So in your widget, you can just do this to return to the previous view:

return RedirectToAction(ViewData["action"].ToString(), ViewData["controller"].ToString());

I am assuming the widget will just be a partial view.

naspinski
Not sure I was clear, but the create widget form is it's own page. Won't your solution just return to the same form? The controller and action RouteData values are those for the current page, not the referring page unless I'm missing something?
Rob