0

コンポーネント アーキテクチャを使用して ASP.Net MVC 2 アプリケーションを構築しています。コンポーネントには 2 つの異なるタイプがあります:部分ビューをレンダリングするコントローラー アクションが関連付けられているElementary Components と、特定のレイアウトですべての子コンポーネント (Elementary Components または Layouts) をレンダリングするLayout Componentsです。

コンポーネント ID を受け取り、適切なビューをレンダリングする汎用のRenderComponent()アクション メソッドを次に示します。

[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>
<%
    }
%>

問題:

3 つの基本的な子コンポーネントが関連付けられたサンプル レイアウト コンポーネントをレンダリングしようとすると、ページがレンダリングされません。一部のデバッグにより、次の問題が明らかになりました。

RenderComponent(5)レイアウト ビューをレンダリングします。レイアウトで最初の子コンポーネントをレンダリングするためHtml.RenderAction("RenderComponent", "Core", 1)に、ビューで呼び出されます。さらに踏み込んで、実際には!!RenderComponent(5)の代わりに呼び出されていることを発見しました。RenderComponent(1)

これは明らかに、レイアウト ビューのレンダリング自体の無限ループにつながります。


なぜこうなった?どうすれば修正できますか?私の階層コンポーネント アーキテクチャは ASP.Net MVC と互換性がありませんか? このようなシステムを ASP.Net MVC でどのように構築しますか?

4

1 に答える 1

0

わかりました、悪いです... もちろん<% Html.RenderAction("RenderComponent", "Core", new { id = child.ID}); %>、VerticalLayout.ascx にある必要があります。

于 2010-11-18T23:04:50.030 に答える