1

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>
KevinIsNowOnline
  • 773
  • 4
  • 10

1 Answers1

0

Simple example to have one page where you display all CDs and each one's content.

Controller:

    public ViewResult Index(){
           return View(db.Cd.Include(x=>x.Content).ToList());
    }

View:

    @model IEnumerable<YourNamespace.Cd>

    @foreach (var cd in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => user.UserName)
                   .... etc ...
        </td>    
        <td>
            @foreach (var c in cd.Content ) {
              <div>
                   @c.contentName 
                   .... etc ...
              </div>
            }
        </td>            
    </tr>
     }

UPDATE #1

change the class to:

 public class Cd
    {
        public int cdID { get; set; }
        public string cdName { get; set; }
        public string tag { get; set; }
        public IList<Content> Contents { get; set; }
    }

Now you have a list of "contents" :)

Dryadwoods
  • 2,875
  • 5
  • 42
  • 72
  • Thanks for the quick answer.. Just a quick thought, shouldn't I be using any ICollection<> Data Type? – KevinIsNowOnline Dec 13 '12 at 08:16
  • For this case there is no problem, for more information take a look in this question: http://stackoverflow.com/questions/3559868/list-ilist-ienumerable-iqueryable-icollection-which-is-most-flexible-return – Dryadwoods Dec 13 '12 at 08:18
  • I tried your suggested solution but it didn't worked. Also, I have tried the following suggestion: http://stackoverflow.com/questions/10229850/asp-net-mvc3-razor-querying-inside-a-model-foreach-within-foreach wherein I modified my class to have a virtual Icollection Reference to the content class but receive the following error:CS1061: 'System.Collections.Generic.IEnumerable<.Models.Cd>' does not contain a definition for 'Content' and no extension method 'Content' accepting a first argument of type 'System.Collections.Generic.IEnumerable<.Models.Cd>' could be found – KevinIsNowOnline Dec 13 '12 at 08:53
  • Could you write the "exception" you are having? – Dryadwoods Dec 13 '12 at 08:55
  • Above, I posted the exception. Here is the logic of the view: CD ID Content @foreach (var cd in Model.Content) { @Html.DisplayFor(modelItem => cd.C) } – KevinIsNowOnline Dec 13 '12 at 08:58