Click here to Skip to main content
15,915,702 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have instantiated a Controller within a Controller. And I have a view defined under the main Controller. Now when I try to use the view from the other controller, it gives me an error saying View not found.


Example:
public HomeController: Controller
{
}
public OtherController: Controller
{
}

View Created: User

Now when I try to use "User" via OtherController, the path turns out to be OtherController/User and it gives me an error saying View not found which is right because that View has been defined for HomeController.

So, is there any way I can access User view from Other Controller.

Any help or idea is appreciated.

Do let me know if anything is unclear.
Thanks.
Posted

1 solution

You just have to state that view name in your return statement:

C#
public ActionResult SomeAction()
{
    // do something
    return View("SharedView", viewModel);
}

If you have a view named SharedView, all actions in that controller can access it. If you want to share it among actions that are on other controllers, you can use the Shared folder in Views or state full path with the view name.
Hope this helps
 
Share this answer
 
Comments
TryAndSucceed 16-Sep-13 12:13pm    
Well, thanks for the help. But what I did was, I gave the entire path for the View and it seems to work. Since, both Controllers were defined in a single .cs file, the view was attached to the main controller and wasn't recognizing it for another controller defined inside. Thanks, anyway.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900