开发者

Pattern for specifying the "return" controller/action in an MVC2 application

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜