Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 758
jaffa Avatar asked Sep 12 '11 14:09

jaffa


People also ask

What is RenderAction in MVC?

RenderAction(HtmlHelper, String) Invokes the specified child action method and renders the result inline in the parent view. RenderAction(HtmlHelper, String, Object) Invokes the specified child action method using the specified parameters and renders the result inline in the parent view.

What is the difference between HTML action and HTML RenderAction?

The difference between the two is that Html. RenderAction will render the result directly to the Response (which is more efficient if the action returns a large amount of HTML) whereas Html. Action returns a string with the result.


1 Answers

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.

like image 104
Richard Dalton Avatar answered Nov 07 '22 20:11

Richard Dalton