将 RenderAction() 与当前操作一起使用时出现问题

发布于 2024-10-03 18:12:58 字数 1783 浏览 3 评论 0原文

我正在使用组件架构构建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯狂的代价 2024-10-10 18:12:58

好吧,我的错...当然它必须是 <% 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文