Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying content under an ID
    primarykey
    data
    text
    <p>I have been studying EF and LINQ for quite a while now and I'm stumped on my inability to gather answer on the process that I'm trying to accomplish below:</p> <p>In the index view, I would like to have a tabular list of all CD with their respective Contents. Please see below classes for more info:</p> <pre><code> public class Cd { public int cdID { get; set; } public string cdName { get; set; } public string tag { get; set; } public Content Content { get; set; } } public class Content { public int contentID { get; set; } public string contentName { get; set; } public string category { get; set; } } </code></pre> <p>Given the classes, how can I achieve what I'm trying to do - display all CD contents under a CD using an cdID?</p> <p>Update #1 - Final Answer ( thanks to DryadWoods)</p> <pre><code>public class Cd { public int cdID { get; set; } public string cdName { get; set; } public string tag { get; set; } public IList&lt;Content&gt; Content { get; set; } //Changes here! No changes in Content class } </code></pre> <p>Final Version of the view:</p> <pre><code>@model IEnumerable&lt;MediaManager.Models.Cd&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt;CD ID&lt;/th&gt; &lt;th&gt;Content&lt;/th&gt; &lt;/tr&gt; @foreach (var cd in Model) //nested loop for displaying the cdID, then proceeds to loop on all contents under certain cdID { &lt;tr&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; cd.cdID) &lt;/td&gt; &lt;td&gt; @foreach (var item in cd.Content) { &lt;p&gt;@item.contentName&lt;/p&gt; } &lt;/td&gt; &lt;/tr&gt; } &lt;/table&gt; </code></pre>
    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.
 

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