title: Loop through list of objects inside object
body: <p>I'm using mvc and I'm passing the class called DataModel as my model to the view:</p>
  
  <pre><code>public class DataModel
  {        
      public List&lt;ServerStats&gt; statsPerMonthPerServer { get; set; }
  }
  </code></pre>
  
  <p>And here is the class ServerStats:</p>
  
  <pre><code>public class ServerStats
  {               
      public string ServerID { get; set; }
      public List&lt;ColumnData&gt; ServerMonthStats { get; set; }
  }
  </code></pre>
  
  <p>And here is the class ColumnData: </p>
  
  <pre><code>public class ColumnData
  {
      public string Date { get; set; }
      public double Sum { get; set; }
  }
  </code></pre>
  
  <p>I have managed to build my ModelData object, if I debug it, it looks like the information I want is in it, but I can not manage to print it out from the view. This is what I've tried to do:</p>
  
  <p>View:</p>
  
  <pre><code>@model IEnumerable&lt;WebApp.Models.DataModel&gt;
  @foreach (var server in Model)
  {
  foreach (var month in server.statsPerMonthPerServer)
  {
      @month.ServerID
      foreach (var column in month.ServerMonthStats)
      {
          @column.Date &lt;br /&gt;
          @column.Sum &lt;br /&gt;
      }
  }    
  }
  </code></pre>
  
  <p>And the controller where I build the DataModel and send it to the view:</p>
  
  <pre><code>DataModel dm = new DataModel();
  dm.statsPerMonthPerServer = new List&lt;ServerStats&gt;();
  
  foreach (var row in serverStats)
          {
                  ServerStats ss = new ServerStats();
                  ss.ServerID = row.ServerID;
                  ss.ServerMonthStats = new List&lt;ColumnData&gt;();
                  ColumnData cd = new ColumnData();
                  cd.Date = row.Year.ToString() + "-" + row.Month.ToString();
                  cd.Sum = row.ServerSum;
                  ss.ServerMonthStats.Add(cd);                    
                  dm.statsPerMonthPerServer.Add(ss);
  
          }
  
  
          return View(dm);
  </code></pre>
  
  <p>The error I get:
  The model item passed into the dictionary is of type 'WebApp.Models.DataModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[WebApp.Models.DataModel]'.</p>
  
  <p>I'm using lists but it says this is a dictionary, but I assume it is talking about my lists. I've read some about the IEnumerable interface. Do I have to implement it on all the classes except DataModel? I thought the model decleration on the top of the view took care of this? Any help would be preciated!</p>