How do I manage MVC areas and RenderAction in different controllers?

13,953

You can specify the area as part of the RouteValueDictionary (or just object of route values) which RenderAction takes as a third parameter in your case:

Html.RenderAction("UserInfo", "Account", new { area = "" });  

This is assuming the Account controller is in the root area.

Share:
13,953
jaffa
Author by

jaffa

Updated on June 07, 2022

Comments

  • jaffa
    jaffa almost 2 years

    I have just added a new Admin area to my project as its started to get quite large and I want to keep it structured.

    I have a _ViewStart.cshtml view which sets a shared layout page to include a menu and a partial with some user information. As this wasn't being added on my area page, I've added the _ViewStart file to my area too.

    This file sets the layout to "~/Views/Shared/_Layout.cshtml", which is outside my Admin area. However, the _Layout file includes a RenderAction() method which calls a Child action method on the controller for rendering. The problem is that the area doesn't seem to have visibility of this controller and so throws the following exception:

    The controller for path '/Admin/LeadOrigin' was not found or does not implement IController.
    

    The point where it occurred though is here:

    Line 70:         <div id="logindisplay">
    Line 71:             @{
    Line 72:                 Html.RenderAction("UserInfo", "Account");              
    Line 73:             }
    Line 74:         </div>
    

    The RenderAction() above works normally as the Account controller is within the scope of the view, whereas in the Admin area, it seems it has no scope of this controller.

    Any ideas how to get round this problem?