2017-10-13 13 views
0

構造体の設定は次のとおりです。 子ドキュメント(DocType)を保持する多くのサービス(DocType)があります。 ドキュメントDocTypeは、その子として他のDocument要素を持つことができます。編集者は、木のようなものを作成するためテンプレートのツリービュー生成のために子を再帰的に取得する

この方法で、それは可能です: サービス>ドキュメント>ドキュメント>ドキュメント>ドキュメント

Documentation center structure

これがために行くことができるレベルの量には制限はありません。

私は各Documentとその子孫を再帰的にループする方法を見つけようとしていますが、それらは互いに入れ子にする必要があります。これは私が困っているところです。

私はコンテンツを生成するために子どもごとの各レベルを再帰的にループする簡単な方法を見つけることはできません。ここで

は、私がこれまで持っているものです。

@{ var selection = Model.Children.Where("Visible"); 
    if (selection.Any()) { 
     <ul> 
      @foreach (var item in selection) { 
      <li> 
       <div class="ServiceDocCenterDocumentation"> 
        <h4><a href="#" id="[email protected]" class="DocCenterDocumentationTitle">@item.Name</a></h4> 
        <div class="DocCenterDocumentationDescription">@item.GetPropertyValue("bodyText")</div> 
       </div> 
       <div> 
        @foreach (var children in item.Descendants()){ 
         @* This is the part I'm struggling with for the last few days *@ 

         var actualChildren = children; 
         <h5><a href="#" id="[email protected]" class="DocCenterDocumentationTitle">@children.Name</a></h5> 
         <div class="DocCenterDocumentationDescription">@children.GetPropertyValue("bodyText")</div> 
        } 
       </div> 
      </li> 
      } 
     </ul> 

    } 
    } 

は、ここで私は(ない手動で、再帰的に)を達成したいものだ。

<div class="myService"> 
    <div class="Documents"> 
     <div class="[email protected]"> 
      @elem.bodyText 
      foreach (var child in elem.Children){ 
       <div class="[email protected]"> 
        @child.bodyText 
        foreach (var grandchild in child.Children){ 
         @* It goes on and on for the amount of levels available *@ 
        } 
       </div> 
      } 
     </div> 
    </div> 
</div> 

私はカミソリでまだ非常に流暢ではありませんよ、私が達成したいものを達成する方法があるかどうかは疑問です。

答えて

1

再帰関数を作成するには、Razorヘルパーを使用する必要があります。

それ以外の場合は、子孫ごとに子孫を使用するべきではありません。この子孫の下にあるすべてのノードが返されるためです。あなたはただその子ども、そして各子供たちの子どもたちなどが必要です。そのため、各子供リストを関数に渡して表示します。一方

@{ var selection = Model.Children.Where("Visible"); 
    if (selection.Any()) { 
     <section class="DocCenter"> 
      @foreach (var item in selection) { 
       <section class="Service"> 
        <div class="ServiceDocCenterDocumentation"> 
         <h1><a href="#" id="[email protected]" class="DocCenterDocumentationTitle">@item.Name</a></h1> 
         <div class="DocCenterDocumentationDescription">@item.GetPropertyValue("bodyText")</div> 
        </div> 

        @DisplayChildren(item.Children().Where("Visible")) 

       </section> 
      } 
     </section> 

    } 
} 

@helper DisplayChildren(IEnumerable<IPublishedContent> children){ 
    if(children.Any()){ 
     <section class="children"> 
      foreach(var child in children){ 
       <section class="[email protected]"> 
        <div class="DocumentDetails"> 
         <h1><a href="#" id="[email protected]" class="DocCenterDocumentationTitle">@children.Name</a></h1> 
         <div class="DocCenterDocumentationDescription">@children.GetPropertyValue("bodyText")</div> 
        </div> 
        //Recursive call here 
        @DisplayChildren(child.Children().Where("Visible")) 
       </section> 
      } 
     </section> 
    } 
} 

、再帰的な質問に関連していないが、私は両親と子供たちを分離する <section>要素、それらがで作成されたとして、あなたはまた、すべての見出しにH1を使用することができる方法を使用しますセクションの階層: Using HTML sections and outlines

+0

私はより良い答えを求めることができませんでした。私はヘルパーを調べるつもりです。それは私のプロジェクト全体を最適化するはずです。私は 'section'要素の機能(と他のHTML5構造要素)についても見ていきます。この回答に23時間で気づく100の報奨金があります(それを割り当てることができるときはいつでも)。ありがとうございました ! –

+0

お手伝いします:) –

関連する問題