title: ASP.NET MVC One to Many Database Table Values with List Attribute
body: <p>My First Table is:</p>
  
  <p>First Table Name: Contacts<br />
  ContactID (PK)<br />
  FirstName<br />
  LastName<br />
  Company<br /></p>
  
  <p>Second Table Name: Phones<br />
  ContactID (FK)<br />
  PhoneType<br />
  PhoneNumber<br /></p>
  
  <p>My view Model is<br /></p>
  
  <pre><code>public class ContactVM2
  {
      public int ContactID { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string Company { get; set; }
  
      public string PhoneType { get; set; }
      public string PhoneNumber { get; set; }
  }
  </code></pre>
  
  <p>Repository Class is</p>
  
  <pre><code>public class ContactRepository
  {
      ContactsDBEntities dbRepo = new ContactsDBEntities();
  
      public List&lt;ContactVM&gt; GetAllContacts()
      {
          List&lt;ContactVM&gt; ContactViewList = new List&lt;ContactVM&gt;();
  
          var allContacts = dbRepo.Contacts.ToList();
          var allPhones = dbRepo.Phones.ToList();
  
          foreach (var cont in allContacts)
          {
              foreach (var ph in allPhones)
              {
                  if (cont.ContactID == ph.ContactID)
                  {
                      ContactViewList.Add(new ContactVM(){
                          ContactID =cont.ContactID,
                          FirstName=cont.FirstName,
                          LastName=cont.LastName,
                          Company=cont.Company,
                          PhoneType=ph.PhoneType,
                          PhoneNumber=ph.PhoneNumber});
                  }
              }
          }
  
          return ContactViewList;
      }        
  }
  </code></pre>
  
  <p>and Controller is</p>
  
  <pre><code>public ActionResult Index()
      {
          ContactRepository contRepo = new ContactRepository();
  
          var allContacts = contRepo.GetAllContacts().ToList();
  
          return View(allContacts);
      }
  </code></pre>
  
  <p>I have following data in Contacts tables<br />
  ContactID &nbsp;&nbsp; FirstName &nbsp; LastName &nbsp; Company<br />
  1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Bill &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Gates &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Microsoft</p>
  
  <p>and in Phones Table<br />
  ContactID &nbsp;&nbsp; PhoneType &nbsp;&nbsp;&nbsp;&nbsp; PhoneNumber<br />
  1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Home&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1111<br />
  1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Office&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2222<br /></p>
  
  <p>I am getting following result<br>
  1 &nbsp;&nbsp; Bill &nbsp;&nbsp; Gates &nbsp;&nbsp; Home &nbsp;&nbsp; 1111<br />
  1 &nbsp;&nbsp; Bill &nbsp;&nbsp; Gates &nbsp;&nbsp; Office &nbsp;&nbsp; 2222<br /></p>
  
  <p>Where Contact details are repeating.<br />
  I need following result<br>
  1 &nbsp;&nbsp; Bill &nbsp;&nbsp; Gates &nbsp;&nbsp; Home &nbsp;&nbsp; 1111<br />
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Office &nbsp;&nbsp; 2222<br />
  I have also tried following changes in View<br /></p>
  
  <pre><code> &lt;td style="border:2px solid Blue;"&gt;
          @{
          foreach (var parent in Model.Where(x=&gt;x.ContactID==item.ContactID).GroupBy(m=&gt;m.PhoneNumber))
         {   
            foreach( var itm in parent )
             {                
               @itm.PhoneNumber &lt;br /&gt;
            }    
            } 
            }
      &lt;/td&gt;
      &lt;td style="border:2px solid red;"&gt;
          @{
          foreach (var parent in Model.Where(x=&gt;x.ContactID==item.ContactID).GroupBy(m=&gt;m.PhoneType))
         {   
            foreach( var itm in parent )
             {                
               @itm.PhoneType &lt;br /&gt;
            }    
            } 
            }
      &lt;/td&gt;
  </code></pre>
  
  <p>But it still repeats the record again.<br />
  Then I tried following changes in ModelView<br /></p>
  
  <pre><code>    public List&lt;string&gt; PhoneType { get; set; }
      public List&lt;string&gt; PhoneNumber { get; set; }
  </code></pre>
  
  <p>but did not get results.<br />
  <strong>Can any one help by giving simplest example as I am at very beginner level.</strong>
  Code without Phone Iteration in View<br /></p>
  
  <pre><code>@foreach (var item in Model) {
  &lt;tr&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.ContactID)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.FirstName)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.LastName)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.Company)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.PhoneType)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.PhoneNumber)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
          @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
          @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
      &lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>