1

Really simple question: I want to be able to redirect from one page to another in Razor MVC.

The user clicks a link on the first page, which calls a method on the first controller. This method uses data from its own model, as well as methods from the second controller, to construct a model for the second controller.

I now wish to display that second view using the newly constructed model. However, there doesn't seem to be a way to use View() to do this (unless I'm blind). The two controllers are in different folders, so it can't automatically find the page to load.

Any ideas?

sapi
  • 9,944
  • 8
  • 41
  • 71

4 Answers4

0

You can use something like

return View("../FolderName/ViewName", model);
santiagoIT
  • 9,411
  • 6
  • 46
  • 57
  • I'm trying to avoid hardcoding it like that. For some reason, when I run that code, it's carrying along the id value that I took as an input to the first controller (ie it goes to ../FolderName/ViewName/id, breaking all the relative paths in the second view :/) – sapi Jan 17 '12 at 04:27
0

It sounds like this has answered this question here: How to simulate Server.Transfer in ASP.NET MVC?

In summary you should look at:

return RedirectToRoute(new { controller = "home"});

or look at some other redirection options:

Community
  • 1
  • 1
Dessus
  • 2,147
  • 1
  • 14
  • 24
  • I did look at redirection, but I couldn't find a redirection option that allowed me to specify the model and the view to redirect to, as I need to do. Did I just miss that? – sapi Jan 17 '12 at 04:39
  • The link I provided, the second one, has a solution. where they pass an object with data, ie a message through a redirect. – Dessus Jan 17 '12 at 04:49
  • That method doesn't work. When redirecting using return RedirectToAction("DisplaySites", new {model = sitesModel, ...}), the value model is null (as are all the others that I attempt to pass). DisplaySites is defined as: public ViewResult DisplaySites(SitesViewModel model, ...) – sapi Jan 17 '12 at 05:12
0

Ideally you cannot return a view from Different Controller and Different Action. But you can return a view from same controller and different action.

To Answer your question, We can always redirect to Different controller and Different Actions. In this scenario it will be a 302 GET request.So if you cannot pass models across Controllers.

(Though you can pass as route values,which will be appended in querystring.(This is not at all advised))

Best approach will be either ,

1) Keep both the actions in same controller.

or

2) Create the model with in the Action of 2nd Controller.

Manas
  • 2,534
  • 20
  • 21
  • The model with the action of the second controller would still need to access the model of the first controller, though, because I need to get data from that in order to build the second model. – sapi Jan 17 '12 at 04:45
  • Then the suggested way would be to keep both the actions in same model. If your 2nd model depends on few results of first model, then better perform the operation on first model in first controller and then redirect to 2nd controller - action with the required result of first model – Manas Jan 17 '12 at 04:56
  • It's the second part there that's not working for me; I can't get the redirect to display the correct model. (As an aside, the two models are displaying data from completely different sources and should not be combined; it just so happens that the second view can give a lot more detail on one of the elements in the first view) – sapi Jan 17 '12 at 05:14
  • OK. If i got it correct, lets your 1st model gives basicproduct information and 2nd fives a detailed information. Then they must be tied with some sort of product Id. If so from the first controller-Action you can return a view. and in the view you can call @Html.Action(...) for 2nd conroller-action passing that identifier. As you mentioned there is some dependency of 2nd model on first, So they are not completely disjoint. ( Better you can give some details on models) – Manas Jan 17 '12 at 05:29
  • The first model reads out from a database of objects all objects that are considered to be at risk of failure (it's a real-time monitoring system). Quite separate from this page I have a condition monitoring page that provides an ordered view of every object in the system (grouped by location -> object type etc). I need to be able to link the first view to the second. All of the information required to do this is in the first view; I just need a way to pass this information to the second view. I want to do it without passing the unique identifier (thus avoiding spurious database queries). – sapi Jan 17 '12 at 05:42
  • I can actually achieve most of this using tempdata, I just realised, but that seems so silly > – sapi Jan 17 '12 at 05:49
0

If the page should redirect to the correct URL for the second controller, I would:

  1. Send to the first controller.
  2. Add model to TempData.
  3. Return RedirectResult to second controller.
  4. Retrieve first model from TempData.
  5. Process and return consolidated model.

If you don't want a redirect, you might consider turning the actions from the first controller into an ActionFilter. That filter can be applied to both controller actions (first and second). Your initial view would post to the second controller directly and the ActionFilter would help create that part of your model.

Henry Fieger
  • 346
  • 3
  • 5