title: Display list which inside an object with viewbag
body: <p>EXEMPLE : </p>
  
  <pre><code>public class country
      {
          public string name {get;set;}
          public string Alias{get;set;}
          public List&lt;city&gt; cities = new List&lt;city&gt;();
  
      }
  </code></pre>
  
  <p>when i create instance of class country on my controller .. in the view i can display only the name and alias using ViewBag. my question is how to access and display all the cities of a country using Viewbag with foreach ..</p>
  
  <p>Tis is my real situation here .. : </p>
  
  <p>In  Models i have : </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Configuration;
  using System.Data;
  using System.Data.SqlClient;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  
      namespace StageApp.Models
      {
          public class DAT
          {
              public int IdDat {get;set;}
              public string AliasDat{get;set;}
              public string IdGare{get;set;}
              public List&lt;Destinataires&gt; destinataires = new List&lt;Destinataires&gt;();
  
          }
            public class DATBussinessLogic
            {
              public void addlistdesti(int iddat , DAT dat)
              {
                    String sql2 = "SELECT matricule,nom_destinataire,prenom_destinataire,etat FROM Destinataires where id_dat =" + dat.IdDat;
                    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["StageConnection"].ConnectionString))
                    {
                        SqlCommand cmd2 = new SqlCommand(sql2, conn);
                        conn.Open();
                        SqlDataReader rdr2 = cmd2.ExecuteReader();
                        while (rdr2.Read())
                        {
                            var destis = new Destinataires();
                            destis.Matricule = rdr2["matricule"].ToString();
                            destis.NomDestinataire = rdr2["nom_destinataire"].ToString();
                            destis.PrenomDestinataire = rdr2["prenom_destinataire"].ToString();
                            destis.Etat = rdr2["etat"].ToString();
                            dat.destinataires.Add(destis);
                        }
                    }
              }
            }
      }
  </code></pre>
  
  <p>And controller i have :</p>
  
  <pre><code>using StageApp.Models;
  using System;
  using System.Collections.Generic;
  using System.Configuration;
  using System.Data.SqlClient;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  
  namespace StageApp.Controllers
  {
      public class DATController : Controller
      {
          DATBussinessLogic DATBL = new DATBussinessLogic();
  
      public ActionResult Index()
          {
              if (Session["username"] == null)
              {
  
                  return RedirectToAction("Index", "Home");
              }
              else
              {
                  ViewBag.name = Session["username"];
                  return View("Index");
              }
          }
  
          public ActionResult Listdat()
          {
              String sql = "SELECT * FROM dat";
              var model = new List&lt;DAT&gt;();
  
              using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["StageConnection"].ConnectionString))
              {
                  SqlCommand cmd = new SqlCommand(sql, conn);
                  conn.Open();
                  SqlDataReader rdr = cmd.ExecuteReader();
                  while (rdr.Read())
                  {
                      var dat = new DAT();
                      dat.IdDat = (int)rdr["id_dat"];
                      dat.AliasDat = rdr["alias_dat"].ToString();
                      dat.IdGare = rdr["id_gare"].ToString();
                      dat.IdGare = DATBL.from_id_to_aliasgare(dat.IdGare);
                      DATBL.addlistdesti(dat.IdDat, dat); //a method from Models
                      model.Add(dat);
  
                  }
  
              }
              ViewBag.dat = model;
              if (Session["username"] == null)
              {
  
                  return RedirectToAction("Index", "Home");
              }
              else
              {
                  ViewBag.name = Session["username"];
                  return View("Listdat");
              }
  
  
          }
      }
  }
  </code></pre>
  
  <p>And this is the view : </p>
  
  <pre><code>&lt;div class="container body-content"&gt;
          &lt;h2&gt;Liste des DAT disponibles !!&lt;/h2&gt;
          &lt;table id="example" class="table table-striped table-bordered" cellspacing="0"&gt;
              &lt;thead&gt;
                  &lt;tr&gt;
                      &lt;th&gt;ID&lt;/th&gt;
                      &lt;th&gt;Alias&lt;/th&gt;
                      &lt;th&gt;Gare&lt;/th&gt;
                      &lt;th&gt;Déstinataires&lt;/th&gt;
  
                  &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
                  @foreach (var item in ViewBag.dat)
                  {
                      &lt;tr&gt;
                          &lt;td&gt;@item.IdDat&lt;/td&gt;
                          &lt;td&gt;@item.AliasDat&lt;/td&gt;
                          &lt;td&gt;@item.IdGare&lt;/td&gt;
                          &lt;td&gt;@foreach (var desti in ViewBag.dat.destinataires)
                              {
                                  &lt;span&gt;Matricule : @desti.Matricule&lt;/span&gt;  
                                  &lt;span&gt;Nom et prénom : @desti.NomDestinataire @desti.PrenomDestinataire&lt;/span&gt;
                                  &lt;span&gt;Etat : @desti.Etat&lt;/span&gt;
                                  &lt;span&gt;***&lt;/span&gt;
                              }
                          &lt;/td&gt;
                      &lt;/tr&gt;
                  }
              &lt;/tbody&gt;
          &lt;/table&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>as you can see i try to display the items of Viewbag.dat .. works fine but Viewbag.dat.destinataires not working </p>
  
  <p>and i got error on the ViewBag.dat.destinataires wich says : </p>
  
  <p>System.Collections.Generic.List' does not contain a definition for "destinataires"</p>
  
  <p>Any ideas where is the problem ?</p>