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:
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:
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; }
}
Given the classes, how can I achieve what I'm trying to do - display all CD contents under a CD using an cdID?
Update #1 - Final Answer ( thanks to DryadWoods)
public class Cd
{
public int cdID { get; set; }
public string cdName { get; set; }
public string tag { get; set; }
public IList<Content> Content { get; set; } //Changes here! No changes in Content class
}
Final Version of the view:
@model IEnumerable<MediaManager.Models.Cd>
<table>
<tr>
<th>CD ID</th>
<th>Content</th>
</tr>
@foreach (var cd in Model) //nested loop for displaying the cdID, then proceeds to loop on all contents under certain cdID
{
<tr>
<td>
@Html.DisplayFor(modelItem => cd.cdID)
</td>
<td>
@foreach (var item in cd.Content)
{
<p>@item.contentName</p>
}
</td>
</tr>
}
</table>