I've created an area that will handle our some generic things across all our development products, just as log ins, HTML helpers, etc. Inside the area, I have a partial view that I'm attempting to reference outside of the area. I've registered the area with
我创建了一个区域,可以处理我们所有开发产品中的一些通用的东西,就像登录,HTML助手等。在该区域内,我有一个局部视图,我试图在该区域之外引用。我已经注册了该地区
public class Routes : AreaRegistration
{
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Common_default",
"Common/{controller}/{action}/{id}",
new {
controller = "Account",
action = "Index",
id = UrlParameter.Optional
});
}
public override string AreaName
{
get { return "MvcCommons"; }
}
}
And now in the regular project, I'm trying to reference a view in the MvcCommons area...
而现在在常规项目中,我正在尝试引用MvcCommons领域中的一个视图......
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>TestGrid</h2>
<% Html.RenderPartial("jQGridTable", ViewData.Model); %>
But I keep getting that the view isn't found. Before, while creating the MVC Commons project, I was getting view errors, but the errors told me that it looked in both the area folders and the default view folders. This time, I'm only getting the default folders. Is there any way to accomplish this?
但我一直认为没有找到视图。之前,在创建MVC Commons项目时,我收到了查看错误,但错误告诉我它在区域文件夹和默认视图文件夹中都有查看。这次,我只获取默认文件夹。有没有办法实现这个目标?
Thanks everyone!
3 个解决方案
#1
5
I haven't actually had to do this, but at a guess I would assume you should use Html.RenderAction()
instead, something like Html.RenderAction("action", "controller", new { area = "Area", model = ViewData.Model })
.
我实际上不必这样做,但我猜你应该假设你应该使用Html.RenderAction(),比如Html.RenderAction(“action”,“controller”,new {area =“Area”,model = ViewData.Model})。
model will have to be replaced with the name of the action's parameters, but that should work.
model必须替换为action的参数名称,但这应该有效。
edit this will require a controller and view setup for each action though.
编辑这将需要一个控制器和视图设置为每个操作。
#2
1
An important thing to remember is that when using RenderPartial you use it in the context of the current action.
需要记住的一件重要事情是,在使用RenderPartial时,您可以在当前操作的上下文中使用它。
As your action isn't in an area it will only look in the View/ folder for the controller the action belongs to then the shared folder.
由于您的操作不在某个区域,因此它只会查看该操作所属的控制器的View /文件夹,然后是共享文件夹。
Any views you wish to share between areas and controllers and have available at the route should be the root View/Shared folder. Really if the view is callable as a partial like that there is probably little reason for it to belong to an area.
您希望在区域和控制器之间共享并且在路径上可用的任何视图应该是根视图/共享文件夹。实际上,如果视图可以作为部分调用,那么它可能没有理由属于某个区域。
You can call into area when you want to render actions rather than partials - which then changes the context of the current action to the action you call into thereby allowing you to then return views within that area.
当您想要渲染动作而不是部分动作时,可以调用区域 - 然后将当前动作的上下文更改为您调用的动作,从而允许您在该区域内返回视图。
#3
0
The Default ViewEngine looks for the Views inside the same area (or root) folders where the user control is referenced. If you want to create a custom way to use or store views, I suggest you to create a custom ViewEngine. Please take a look at this example: Implement Theme Folders using a Custom ViewEngine
默认ViewEngine在引用用户控件的相同区域(或根)文件夹中查找视图。如果要创建自定义方式来使用或存储视图,建议您创建自定义ViewEngine。请看一下这个例子:使用Custom ViewEngine实现主题文件夹
#1
5
I haven't actually had to do this, but at a guess I would assume you should use Html.RenderAction()
instead, something like Html.RenderAction("action", "controller", new { area = "Area", model = ViewData.Model })
.
我实际上不必这样做,但我猜你应该假设你应该使用Html.RenderAction(),比如Html.RenderAction(“action”,“controller”,new {area =“Area”,model = ViewData.Model})。
model will have to be replaced with the name of the action's parameters, but that should work.
model必须替换为action的参数名称,但这应该有效。
edit this will require a controller and view setup for each action though.
编辑这将需要一个控制器和视图设置为每个操作。
#2
1
An important thing to remember is that when using RenderPartial you use it in the context of the current action.
需要记住的一件重要事情是,在使用RenderPartial时,您可以在当前操作的上下文中使用它。
As your action isn't in an area it will only look in the View/ folder for the controller the action belongs to then the shared folder.
由于您的操作不在某个区域,因此它只会查看该操作所属的控制器的View /文件夹,然后是共享文件夹。
Any views you wish to share between areas and controllers and have available at the route should be the root View/Shared folder. Really if the view is callable as a partial like that there is probably little reason for it to belong to an area.
您希望在区域和控制器之间共享并且在路径上可用的任何视图应该是根视图/共享文件夹。实际上,如果视图可以作为部分调用,那么它可能没有理由属于某个区域。
You can call into area when you want to render actions rather than partials - which then changes the context of the current action to the action you call into thereby allowing you to then return views within that area.
当您想要渲染动作而不是部分动作时,可以调用区域 - 然后将当前动作的上下文更改为您调用的动作,从而允许您在该区域内返回视图。
#3
0
The Default ViewEngine looks for the Views inside the same area (or root) folders where the user control is referenced. If you want to create a custom way to use or store views, I suggest you to create a custom ViewEngine. Please take a look at this example: Implement Theme Folders using a Custom ViewEngine
默认ViewEngine在引用用户控件的相同区域(或根)文件夹中查找视图。如果要创建自定义方式来使用或存储视图,建议您创建自定义ViewEngine。请看一下这个例子:使用Custom ViewEngine实现主题文件夹