将 RenderAction() 与当前操作一起使用时出现问题
我正在使用组件架构构建 ASP.Net MVC 2 应用程序。有两种不同类型的组件:基本组件,它具有渲染部分视图的关联控制器操作;布局组件,它渲染所有子组件(基本组件或再次布局)在一定的布局中。
这是我的通用 RenderComponent() 操作方法,它采用组件 ID 并呈现适当的视图:
[ChildActionOnly]
public ActionResult RenderComponent(int id)
{
ComponentRepository repository = new ComponentRepository();
Component component = repository.GetComponent(id);
if (component.ControllerName != null && component.ViewName != null)
{
// render elementary component
return PartialView("Elementary", component);
}
else
{
// render layout
return PartialView(component.Layout.ViewName, component);
}
}
Elementary.ascx 呈现基本组件:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyApp.Models.Component>" %>
<% Html.RenderAction(Model.ViewName, Model.ControllerName); %>
目前唯一存在的布局是VerticalLayout.ascx:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyApp.Models.Component>" %>
<%
foreach (var child in Model.ChildComponents()) {
%>
<div class="layout-container">
<% Html.RenderAction("RenderComponent", "Core", child.ID); %>
</div>
<%
}
%>
问题:
当我尝试渲染具有三个关联的基本子组件的示例布局组件时,页面不会渲染。一些调试揭示了以下问题:
RenderComponent(5)
渲染布局视图。 为了渲染布局中的第一个子组件,在视图中调用 Html.RenderAction("RenderComponent", "Core", 1)
。更进一步,我发现实际上调用了 RenderComponent(5)
而不是 RenderComponent(1)
!
这显然会导致布局视图渲染本身的无限循环。
为什么会发生这种情况?我该如何修复它?我的分层组件架构与 ASP.Net MVC 不兼容吗?您将如何在 ASP.Net MVC 中构建这样的系统?
I'm building an ASP.Net MVC 2 application with a component architecture. There are two different types of components: Elementary Components, which have an associated controller action rendering a partial view, and Layout Components, which render all their child components (Elementary Components or again Layouts) in a certain layout.
Here is my generic RenderComponent() action method, which takes a component ID and renders the appropriate view:
[ChildActionOnly]
public ActionResult RenderComponent(int id)
{
ComponentRepository repository = new ComponentRepository();
Component component = repository.GetComponent(id);
if (component.ControllerName != null && component.ViewName != null)
{
// render elementary component
return PartialView("Elementary", component);
}
else
{
// render layout
return PartialView(component.Layout.ViewName, component);
}
}
Elementary.ascx renders an elementary component:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyApp.Models.Component>" %>
<% Html.RenderAction(Model.ViewName, Model.ControllerName); %>
Currently the only existing layout is the VerticalLayout.ascx:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyApp.Models.Component>" %>
<%
foreach (var child in Model.ChildComponents()) {
%>
<div class="layout-container">
<% Html.RenderAction("RenderComponent", "Core", child.ID); %>
</div>
<%
}
%>
The Problem:
When I tried to render an example layout component with three associated elementary child components, the page wouldn't render. Some debugging revealed the following problem:
RenderComponent(5)
renders the layout view.
For rendering the first child component in the layout, Html.RenderAction("RenderComponent", "Core", 1)
is called in the view. Stepping further, I discovered that in effect RenderComponent(5)
is called instead of RenderComponent(1)
!!
This obviously results in an infinite loop of the layout view rendering itself.
Why is this happening? How can I fix it? Is my hierarchical component architecture incompatible with ASP.Net MVC? How would you build such a system in ASP.Net MVC?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我的错...当然它必须是
<% Html.RenderAction("RenderComponent", "Core", new { id = child.ID}); VerticalLayout.ascx 中的 %>
。OK, my bad... Of course it has to be
<% Html.RenderAction("RenderComponent", "Core", new { id = child.ID}); %>
in VerticalLayout.ascx.