281

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

332

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.

7
  • 1
    In the situation you describe above, yes, you should be using the Shared folder.
    – Jon
    Commented May 18, 2009 at 21:58
  • 76
    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.
    – womp
    Commented May 18, 2009 at 22:03
  • 2
    Thanks for the explaination. I didn't know Views could be called like that. The Shared directory of course works perfectly :)
    – dtc
    Commented May 19, 2009 at 20:28
  • 2
    I wasn't in a Controller, so I had to use new ViewResult { ViewName = "~/Views/Error/Unauthorised.cshtml" }; and it worked
    – Nacht
    Commented Jul 18, 2017 at 1:55
  • 1
    This solution leaves the URL unchanged (the view which will be rendered won't correspond to the displayed URL in the browser)!
    – Mr. L
    Commented Dec 12, 2019 at 8:15
175

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.

4
  • 4
    Works in MVC 2, and turned out to be the cleanest solution for an unusual situation I'm dealing with. Commented Feb 2, 2011 at 21:33
  • 3
    Resharper will report that link as an error but it still works. Commented Feb 15, 2012 at 1:00
  • 1
    @CodeMonkeyKing - Resharper 7 (in VS2012) correctly identifies a path that is formatted as "~/Views/Category/NotFound.cshtml".
    – Richard Ev
    Commented Aug 28, 2013 at 9:59
  • Worth noting: this solution works with display modes, e.g. if you had a "/Category/NotFound.Mobile.cshtml" view. Commented Dec 30, 2015 at 17:29
79

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

return RedirectToAction("ActionOrViewName", "ControllerName");
4
  • 1
    I believe this should go as RedirectToAction("ActionOrView", "Controller", null) as otherwise second paramater is routeValues
    – Zoran P.
    Commented Dec 20, 2014 at 8:11
  • But in this case, you have to actually write the action in the controller, unlike with the View solution.
    – tobbenb3
    Commented Mar 5, 2019 at 17:13
  • @tobbenb3 That's a much better solution as opposed to hardcoding a result. Either way you shouldn't be passing in a path string.
    – perustaja
    Commented Feb 14, 2020 at 22:54
  • 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 Commented May 1 at 19:14
35

Have you tried RedirectToAction?

9
  • 1
    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
    – yoel halb
    Commented Dec 4, 2012 at 18:10
  • 4
    how about without redirecting?
    – Luckyy
    Commented Nov 7, 2013 at 10:58
  • way better solution than moving the view to shared folders
    – juFo
    Commented Apr 10, 2014 at 9:22
  • 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. Commented Apr 17, 2014 at 18:55
  • 3
    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. Commented Aug 23, 2014 at 13:30
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
  • how would i send an object with this approach?
    – Djeroen
    Commented Jan 23, 2016 at 17:10
  • Take a look @Djeroen Commented Feb 16, 2016 at 23:55
  • 6
    You are not returning a view, you are calling an action.
    – 0014
    Commented Aug 31, 2017 at 18:24
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
  • I did not know that. That sounds like it's something I might use in the future. Yes, mvc rocks :)
    – dtc
    Commented May 20, 2009 at 18:33
1

With this code you can obtain any controller:

var controller = DependencyResolver.Current.GetService<ControllerB>();
controller.ControllerContext = new ControllerContext(this.Request.RequestContext, 
controller);
2
  • Translation: With this code you can obtain any controller: [code], Regards, Commented Sep 7, 2018 at 2:26
  • 3
    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.
    – Nic3500
    Commented Sep 7, 2018 at 3:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.