[asp.net] Display a view from another controller in ASP.NET MVC

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?

This question is related to asp.net asp.net-mvc views controllers

The answer is


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.


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

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

With this code you can obtain any controller:

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

Have you tried RedirectToAction?


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...


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" });

Examples related to asp.net

RegisterStartupScript from code behind not working when Update Panel is used You must add a reference to assembly 'netstandard, Version=2.0.0.0 No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization How to use log4net in Asp.net core 2.0 Visual Studio 2017 error: Unable to start program, An operation is not legal in the current state How to create roles in ASP.NET Core and assign them to users? How to handle Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause() ASP.NET Core Web API Authentication Could not load file or assembly 'CrystalDecisions.ReportAppServer.CommLayer, Version=13.0.2000.0 WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery

Examples related to asp.net-mvc

Using Lato fonts in my css (@font-face) Better solution without exluding fields from Binding Vue.js get selected option on @change You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to send json data in POST request using C# VS 2017 Metadata file '.dll could not be found The default XML namespace of the project must be the MSBuild XML namespace How to create roles in ASP.NET Core and assign them to users? The model item passed into the dictionary is of type .. but this dictionary requires a model item of type How to use npm with ASP.NET Core

Examples related to views

Attempt to present UIViewController on UIViewController whose view is not in the window hierarchy How to find which views are using a certain table in SQL Server (2008)? View's SELECT contains a subquery in the FROM clause What is the difference between a stored procedure and a view? Android: How to get a custom View's height and width? How to create EditText with rounded corners? Can we pass parameters to a view in SQL? In Oracle, is it possible to INSERT or UPDATE a record through a view? Display a view from another controller in ASP.NET MVC What is a View in Oracle?

Examples related to controllers

How to pass model attributes from one Spring MVC controller to another controller? Display a view from another controller in ASP.NET MVC