Display a view from another controller in ASP.NET MVC

285,053

Solution 1

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.

Solution 2

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.

Solution 3

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

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

Solution 4

Have you tried RedirectToAction?

Solution 5

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" });
Share:
285,053

Related videos on Youtube

dtc
Author by

dtc

Updated on May 02, 2020

Comments

  • dtc
    dtc about 4 years

    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?

  • dtc
    dtc about 15 years
    Thanks, this sounds like the solution I should be using. I still wonder if it is possible to display a View from another controller though. I'm guessing this violates some rule in MVC?
  • Jonathan Freeland
    Jonathan Freeland about 15 years
    In the situation you describe above, yes, you should be using the Shared folder.
  • nuiun
    nuiun about 15 years
    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.
  • dtc
    dtc about 15 years
    Thanks for the explaination. I didn't know Views could be called like that. The Shared directory of course works perfectly :)
  • Todd Menier
    Todd Menier over 13 years
    Works in MVC 2, and turned out to be the cleanest solution for an unusual situation I'm dealing with.
  • CodeMonkeyKing
    CodeMonkeyKing over 12 years
    Resharper will report that link as an error but it still works.
  • yoel halb
    yoel halb over 11 years
    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
  • Richard Ev
    Richard Ev almost 11 years
    @CodeMonkeyKing - Resharper 7 (in VS2012) correctly identifies a path that is formatted as "~/Views/Category/NotFound.cshtml".
  • Luckyy
    Luckyy over 10 years
    how about without redirecting?
  • juFo
    juFo about 10 years
    way better solution than moving the view to shared folders
  • Jan Zahradník
    Jan Zahradník about 10 years
    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.
  • NightOwl888
    NightOwl888 almost 10 years
    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.
  • Zoran P.
    Zoran P. over 9 years
    I believe this should go as RedirectToAction("ActionOrView", "Controller", null) as otherwise second paramater is routeValues
  • Djeroen
    Djeroen over 8 years
    how would i send an object with this approach?
  • Ignacio Chiazzo
    Ignacio Chiazzo over 8 years
    Take a look @Djeroen
  • Nacht
    Nacht almost 7 years
    I wasn't in a Controller, so I had to use new ViewResult { ViewName = "~/Views/Error/Unauthorised.cshtml" }; and it worked
  • 0014
    0014 almost 7 years
    You are not returning a view, you are calling an action.
  • Robert Columbia
    Robert Columbia almost 6 years
    Translation: With this code you can obtain any controller: [code], Regards,
  • Nic3500
    Nic3500 almost 6 years
    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.
  • nulltron
    nulltron over 5 years
    Couldn't you modify the Response.StatusCode to the proper status code in the view? Or would the status code be read by the search engine before this modification take place?
  • Paul Johnson
    Paul Johnson over 5 years
    @NightOwl888 good point about the 404. I'm not sure if he would want a 404 status though since he has a page for it. Although, its an interesting point. I would say if the categories are dynamic then maybe 404 is not a good idea? This maybe like having a 404 if you searched for keywords that yielded no results.
  • NightOwl888
    NightOwl888 over 5 years
    @NullReff - No, each request only returns a single HTTP response. In the case of RedirectToAction, the first request will return a 302, instructing the browser to do a second request for the URL (strictly speaking, the browser doesn't have to follow this instruction, it is out of your control). If a search engine does follow a 302 redirect to a 404 not found response, this is a confusing set of instructions, and a wasteful and unnecessary round trip.
  • NightOwl888
    NightOwl888 over 5 years
    @PaulJohnson - There would really be nothing wrong with displaying a custom page and also providing a 404 status code. The HTTP status and the page that is displayed are independent. However, making the status 302 when you meant to provide a 404 is a wasteful approach. If you want your application to show a 404 for a certain URL, you should route to that action, not redirect there. That is, make one round trip, not two.
  • Mr. L
    Mr. L over 4 years
    This solution leaves the URL unchanged (the view which will be rendered won't correspond to the displayed URL in the browser)!