282

Is it possible to display a view from another controller?

Say for example I have a CategoriesController and a Category/NotFound.aspx view. While in the CategoriesController, I can easly return View("NotFound").

Now say I have a ProductsController and an action and view to add a product. However, this action requires that we have a Category to add the Product to. For example, Products/Add/?catid=10.

If I am not able to find the Category based on catid, I want to show the NotFound view from the Categories controller instead of creating a CategoryNotFound view under the Products controller.

Is this possible or am I structuring things in the wrong way? Is there a good way to do this?

7 Answers 7

333

Yes. By default, ASP.NET MVC checks first in \Views\[Controller_Dir]\, but after that, if it doesn't find the view, it checks in \Views\Shared.

The shared directory is there specifically to share Views across multiple controllers. Just add your View to the Shared subdirectory and you're good to go.

If you do return View("~/Views/Wherever/SomeDir/MyView.aspx") You can return any View you'd like.

Sign up to request clarification or add additional context in comments.

7 Comments

In the situation you describe above, yes, you should be using the Shared folder.
Yep, that is also possible. If you do return View("~/Views/Wherever/SomeDir/MyView.aspx") You can return any View you'd like. This doesn't violate any particular rule per se, however, ASP.Net MVC is all about "convention over congfiguration". In other words, the framework is built to operate automatically using certain conventions, and you should utilize it where possible.
Thanks for the explaination. I didn't know Views could be called like that. The Shared directory of course works perfectly :)
I wasn't in a Controller, so I had to use new ViewResult { ViewName = "~/Views/Error/Unauthorised.cshtml" }; and it worked
This solution leaves the URL unchanged (the view which will be rendered won't correspond to the displayed URL in the browser)!
|
178

You can use:

return View("../Category/NotFound", model);

It was tested in ASP.NET MVC 3, but should also work in ASP.NET MVC 2.

5 Comments

Works in MVC 2, and turned out to be the cleanest solution for an unusual situation I'm dealing with.
Resharper will report that link as an error but it still works.
@CodeMonkeyKing - Resharper 7 (in VS2012) correctly identifies a path that is formatted as "~/Views/Category/NotFound.cshtml".
Worth noting: this solution works with display modes, e.g. if you had a "/Category/NotFound.Mobile.cshtml" view.
You may want to add ControllerContext.RouteData.Values["controller"] = "Category" before returning the view, so that the view engine can find any non-shared partial views etc... (this is in .net 8 (core) mvc).
80

Yes its possible. Return a RedirectToAction() method like this:

return RedirectToAction("ActionOrViewName", "ControllerName");

4 Comments

I believe this should go as RedirectToAction("ActionOrView", "Controller", null) as otherwise second paramater is routeValues
But in this case, you have to actually write the action in the controller, unlike with the View solution.
@tobbenb3 That's a much better solution as opposed to hardcoding a result. Either way you shouldn't be passing in a path string.
I think that using RedirectToAction() you run all the code in the action, but using View() you call the file directly without running the controller
35

Have you tried RedirectToAction?

9 Comments

I would say that this is the MVC for anyone that doesn't want the view int he shared folders, note that for all other solutions (such as using direct paths) anyone trying to re-factor the views will not have to take in mind that it is also being used in another controller, resulting in unpredictable behavior
how about without redirecting?
way better solution than moving the view to shared folders
This solution requires action on controller, View(directPath) renders output without any action. When you add an action, you need to think to hide it from direct access via url, it will generate step in browser history etc. But yes, it's my way to go.
RedirectToAction sends a 302 response code to the browser. That is not appropriate when you are trying to show a 404 not found page. That is, this solution appears to work but will confuse search engines.
|
24

Yes, you can. Return an Action like this :

return RedirectToAction("View", "Name of Controller");

An example:

return RedirectToAction("Details/" + id.ToString(), "FullTimeEmployees");

This approach will call the GET method

Also you could pass values to action like this:

return RedirectToAction("Details/" + id.ToString(), "FullTimeEmployees", new {id = id.ToString(), viewtype = "extended" });

3 Comments

how would i send an object with this approach?
Take a look @Djeroen
You are not returning a view, you are calling an action.
5

You can also call any controller from JavaScript/jQuery. Say you have a controller returning 404 or some other usercontrol/page. Then, on some action, from your client code, you can call some address that will fire your controller and return the result in HTML format your client code can take this returned result and put it wherever you want in you your page...

1 Comment

I did not know that. That sounds like it's something I might use in the future. Yes, mvc rocks :)
1

With this code you can obtain any controller:

var controller = DependencyResolver.Current.GetService<ControllerB>();
controller.ControllerContext = new ControllerContext(this.Request.RequestContext, 
controller);

2 Comments

Translation: With this code you can obtain any controller: [code], Regards,
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.