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

2

33

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?

Vermin answered 12/9, 2011 at 14:57 Comment(0)
R
61

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.

Ribera answered 12/9, 2011 at 15:2 Comment(0)
M
0

You can use @Html.Action to render partial view by passing your parameter values if required.

@Html.Action("ActionName", "ControolerName", new { firstParam = "a", SecondParam = "b",.... })
Malayoindonesian answered 27/9, 2019 at 17:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.