Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would start with adding three models: A, B and C. In these models, A contains a List of B's and B contains a List of C's. For testing purposes, I would also add something to uniquely identify them, such as a Name. Example:</p> <pre><code>public class A { public string Name { get; set; } public List&lt;B&gt; Bs { get; set; } } public class B { public string Name { get; set; } public List&lt;C&gt; Cs { get; set; } } public class C { public string Name { get; set; } } </code></pre> <p>In my controller, I would add an action method that fills these models with data. For testing purposes, I'm using dummy data.</p> <pre><code>public ActionResult List() { C c1 = new C() { Name = "C1" }; C c2 = new C() { Name = "C2" }; C c3 = new C() { Name = "C3" }; C c4 = new C() { Name = "C4" }; B b1 = new B() { Name = "B1", Cs = new List&lt;C&gt;() { c1, c3 } }; B b2 = new B() { Name = "B2", Cs = new List&lt;C&gt;() { c2, c4 } }; B b3 = new B() { Name = "B3", Cs = new List&lt;C&gt;() { c1, c2, c4 } }; B b4 = new B() { Name = "B4", Cs = new List&lt;C&gt;() { c1, c2, c3, c4 } }; A a1 = new A() { Name = "A1", Bs = new List&lt;B&gt;() { b1, b2 } }; A a2 = new A() { Name = "A2", Bs = new List&lt;B&gt;() { b3, b4 } }; A a3 = new A() { Name = "A3", Bs = new List&lt;B&gt;() { b1, b2, b3, b4 } }; List&lt;A&gt; listOfAs = new List&lt;A&gt;() { a1, a2, a3 }; return View(listOfAs); } </code></pre> <p>And for the final piece, the view to display all of this. Simply using @foreach in a nested manner gives us what we need:</p> <pre><code>@model IEnumerable&lt;HelloMvc.Models.A&gt; &lt;h2&gt;List of As&lt;/h2&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt;&lt;/th&gt; &lt;/tr&gt; @foreach (var a in Model) { &lt;tr&gt; &lt;td&gt; @a.Name&lt;br /&gt; @foreach (var b in a.Bs) { @b.Name&lt;br /&gt; foreach (var c in b.Cs) { @c.Name&lt;br /&gt; } } &lt;/td&gt; &lt;/tr&gt; } </code></pre> <p></p> <p>Note that the "HelloMvc" is my namespace. You probably need to change this to your own namespace.</p> <p>Everything put together, this results in a list of A's with nested B's and C's:</p> <pre><code>A1 B1 C1 C3 B2 C2 C4 A2 B3 C1 C2 C4 B4 C1 C2 C3 C4 A3 B1 C1 C3 B2 C2 C4 B3 C1 C2 C4 B4 C1 C2 C3 C4 </code></pre> <p>(You'll have to imagine some styling.) Hope this example helps you.</p> <p>(sidenote) Funny how that third foreach doesn't have a '@' in front of it. It's not a typo or an error, if I put it there, an exception is raised. Small bug in the Razor view engine, I suspect.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload