如何管理不同控制器中的 MVC 区域和 RenderAction?
我刚刚向我的项目添加了一个新的管理区域,因为它开始变得相当大,我想保持它的结构化。
我有一个 _ViewStart.cshtml 视图,它设置共享布局页面以包括菜单和带有一些用户信息的部分。由于这没有添加到我的区域页面上,因此我也将 _ViewStart 文件添加到了我的区域中。
该文件将布局设置为“~/Views/Shared/_Layout.cshtml”,该布局位于我的管理区域之外。 但是,_Layout 文件包含一个 RenderAction() 方法,该方法调用控制器上的 Child 操作方法进行渲染。问题是该区域似乎没有该控制器的可见性,因此抛出以下异常:
The controller for path '/Admin/LeadOrigin' was not found or does not implement IController.
但发生的点在这里:
Line 70: <div id="logindisplay">
Line 71: @{
Line 72: Html.RenderAction("UserInfo", "Account");
Line 73: }
Line 74: </div>
上面的 RenderAction() 正常工作,因为帐户控制器在视图的范围内,而在管理区域中,似乎没有该控制器的范围。
有什么想法可以解决这个问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将区域指定为 RouteValueDictionary (或只是路由值的对象)的一部分,在您的情况下,RenderAction 将其作为第三个参数:
这是假设帐户控制器位于根区域中。
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:
This is assuming the Account controller is in the root area.
如果需要,您可以使用 @Html.Action 通过传递参数值来呈现部分视图。
You can use @Html.Action to render partial view by passing your parameter values if required.