Display a view from another controller in ASP.NET MVC
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" });
Related videos on Youtube
dtc
Updated on May 02, 2020Comments
-
dtc about 4 years
Is it possible to display a view from another controller?
Say for example I have a
CategoriesController
and aCategory/NotFound.aspx
view. While in theCategoriesController
, I can easly returnView("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 theNotFound
view from the Categories controller instead of creating aCategoryNotFound
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 about 15 yearsThanks, 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 about 15 yearsIn the situation you describe above, yes, you should be using the Shared folder.
-
nuiun about 15 yearsYep, 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 about 15 yearsThanks for the explaination. I didn't know Views could be called like that. The Shared directory of course works perfectly :)
-
Todd Menier over 13 yearsWorks in MVC 2, and turned out to be the cleanest solution for an unusual situation I'm dealing with.
-
CodeMonkeyKing over 12 yearsResharper will report that link as an error but it still works.
-
yoel halb over 11 yearsI 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 almost 11 years@CodeMonkeyKing - Resharper 7 (in VS2012) correctly identifies a path that is formatted as
"~/Views/Category/NotFound.cshtml"
. -
Luckyy over 10 yearshow about without redirecting?
-
juFo about 10 yearsway better solution than moving the view to shared folders
-
Jan Zahradník about 10 yearsThis 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 almost 10 yearsRedirectToAction 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. over 9 yearsI believe this should go as RedirectToAction("ActionOrView", "Controller", null) as otherwise second paramater is routeValues
-
Djeroen over 8 yearshow would i send an object with this approach?
-
Ignacio Chiazzo over 8 yearsTake a look @Djeroen
-
Nacht almost 7 yearsI wasn't in a Controller, so I had to use
new ViewResult { ViewName = "~/Views/Error/Unauthorised.cshtml" };
and it worked -
0014 almost 7 yearsYou are not returning a view, you are calling an action.
-
Robert Columbia almost 6 yearsTranslation: With this code you can obtain any controller: [code], Regards,
-
Nic3500 almost 6 yearsWhile 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 over 5 yearsCouldn'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 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 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 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 over 4 yearsThis solution leaves the URL unchanged (the view which will be rendered won't correspond to the displayed URL in the browser)!