title: How to loop through two viewbag items on View pages in MVC 4.0
body: <p>I want to display a table in the MVC 4.0 View page that has following code:</p>
  
  <pre><code>&lt;table &gt;
    &lt;thead&gt;           
      &lt;tr&gt;
        &lt;th&gt;Student Name&lt;/th&gt;
        &lt;th&gt;Gaurdian&lt;/th&gt;
        &lt;th&gt;Associate Teacher&lt;/th&gt;
  
      &lt;/tr&gt;
    &lt;/thead&gt;
  
    @foreach(var stud in ViewBag.students)
    { 
       &lt;tr&gt;
  
           &lt;td&gt;@stud.Name&lt;/td&gt;
       &lt;/tr&gt;
     }
  
  &lt;/table&gt;
  </code></pre>
  
  <p>This works fine.But , the Guardian information and the associate teacher information is put in different ViewBag objects like ViewBag.guardians and Viewbag.assoc. How should i loop through them to display them in required table cells??</p>
  
  <p>Loops within the loop like</p>
  
  <pre><code>   @foreach(var student in ViewBag.students)
    { 
      foreach(var gaurdian in ViewBag.guardians)
      {     
        &lt;tr&gt;
  
          &lt;td&gt;@student.Name&lt;/td&gt;}
          &lt;td&gt;@guardian.Name&lt;/td&gt;
        &lt;/tr&gt;
      }
    }
  </code></pre>
  
  <p>sounds to be ridiculous. Please provide me proper solution.
  The student class contains guardian field but it has its own another class as follows:</p>
  
  <pre><code>      public class Student
        {
           public string Name {get;set;}
           public string RollNo {get;set;}
           public virtual Guardian Guardian {get;set;}
           public IList&lt;Guardian&gt; GuardianName {get;set;}
        }
         public class Guardian
        {
           public string Name{get;set;}
           public string MobNumber{get;set;}
         }  
        public class Associate
         {
  
            public string AID{get;set;}
            public virtual Student Student{get;set;}
            public string RollNo {get;set;}
         }            
  </code></pre>