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:
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?
jfschwarz
Member
1 Points
1 Post
Hierarchical Component Architecture in MVC
Nov 17, 2010 11:19 PM|LINK
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:
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 effectRenderComponent(5)is called instead ofRenderComponent(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?
ASP.NET MVC asp.net
ignatandrei
All-Star
135085 Points
21668 Posts
Moderator
MVP
Re: Hierarchical Component Architecture in MVC
Nov 18, 2010 06:38 AM|LINK
please call
Html.RenderAction("RenderComponent", "Core",new {id= 1})