title: visual studio mvc 5 creating table with data from different models/tables
body: <p>I recently started using the vs mvc 5 framework and most tutorials say how to use a single data source for a table in a view, I however would like to make a table in a view which takes data from 2-3 different tables and connects them together, I tried doing it basing on some questions on stackoverflow etc. but nothing seems to works.</p>
  
  <p>There are 3 tables: Contattis, Companies and Contacts, the first one is an sql server table connected with visual studio as .edmx, the last 2 are models created in the application and seeded through it. I want to connect them based on Contact ID.</p>
  
  <p>right at this moment this is the code:</p>
  
  <p>Model:</p>
  
  <pre><code>namespace ContactManager.Models
  {
  public partial class ContattiDB2 : DbContext
  {
      public System.Data.Entity.DbSet&lt;ContactManager.Models.Contatti&gt; Contatti { get; set; }
      public System.Data.Entity.DbSet&lt;ContactManager.Models.ContactsUni2&gt; ContactsUni2 { get; set; }
  }
  
  public partial class ContactsUnified
  {
      //these are the 3 tables I want to use
      public IEnumerable&lt;Contatti&gt; Contattis { get; set; }
      public IEnumerable&lt;Companies&gt; Companies { get; set; }
      public IEnumerable&lt;Contact&gt; Contacts { get; set; }
  }
  
  }
  </code></pre>
  
  <p>Companies:</p>
  
  <pre><code>namespace ContactManager.Models
  {
  public class Companies
  {
      [Key]
      public int CompanyId { get; set; }
      public string CompanyName { get; set; }
      public string CompanyAddress { get; set; }
      public string CompanyCity { get; set; }
      public string CompanyState { get; set; }
      public string CompanyZip { get; set; }
      public string CompanyArea { get; set; }
  }
  }
  </code></pre>
  
  <p>Contacts:</p>
  
  <pre><code>namespace ContactManager.Models
  {
  public class Contact
  {
      [Key]
      public int ContactId { get; set; }
      public string Name { get; set; }
      public string Address { get; set; }
      public string City { get; set; }
      public string State { get; set; }
      public string Zip { get; set; }
      [DataType(DataType.EmailAddress)]
      public string Email { get; set; }
      public int CompanyId { get; set; }
  }
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>namespace ContactManager.Controllers
  {
  public class ContactsUnifiedController : Controller
  {
      private ContattiDB2 db2 = new ContattiDB2();
      private ApplicationDbContext db = new ApplicationDbContext();
  
      // GET: ContactsUnified
      public ActionResult Index()
      {
          /*var contacts = db.Contacts.ToList();
          var companies = db.Companies.ToList();
          var contatti = db2.Contattis.ToList();
          var view = new ContactsUnified()
          {
              Contacts = contacts,
              Companies = companies,
              Contattis = contatti
          };
          return View(view);*/
  
          var viewModel = new ContactsUni2();
          viewModel.Contattis = db.Contattis
              .Include(i =&gt; i.ContattoID);
          viewModel.Contacts = db.Contacts;
          viewModel.Companies = db.Companies;
          return View(viewModel);
      }
  </code></pre>
  
  <p>The view (This method doesn't work, it says "The model item passed into the dictionary is of type 'ContactManager.Models.ContactsUnified', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[ContactManager.Models.ContactsUnified]'." Though even if it did work I doubt it would display the 3 tables data as required)</p>
  
  <pre><code>@model IEnumerable&lt;ContactManager.Models.ContactsUnified&gt;
  
  @{
     ViewBag.Title = "Index";
   }
  
  &lt;h2&gt;Contacts Unified&lt;/h2&gt;
  
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta name="viewport" content="width=device-width" /&gt;
  &lt;title&gt;Index&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;p&gt;
      @Html.ActionLink("Create New", "Create")
  &lt;/p&gt;
  &lt;table class="table"&gt;
      &lt;tr&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Contatti.Nome)
              @Html.DisplayNameFor(model =&gt; model.Contatti.Citta)
              @Html.DisplayNameFor(model =&gt; model.Contatti.ContattoID)
              @Html.DisplayNameFor(model =&gt; model.Contatti.CodicePostale)
              @Html.DisplayNameFor(model =&gt; model.Contatti.Email)
              @Html.DisplayNameFor(model =&gt; model.Contact.Address)
              @Html.DisplayNameFor(model =&gt; model.Contact.CompanyId)
              @Html.DisplayNameFor(model =&gt; model.Contact.ContactId)
              @Html.DisplayNameFor(model =&gt; model.Company.CompanyName)
  
          &lt;/th&gt;
          &lt;th&gt;&lt;/th&gt;
      &lt;/tr&gt;
  
  @foreach (var item in Model) {
      &lt;tr&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.Contatti.Nome)
              @Html.DisplayFor(modelItem =&gt; item.Contatti.Citta)
              @Html.DisplayFor(modelItem =&gt; item.Contatti.ContattoID)
              @Html.DisplayFor(modelItem =&gt; item.Contatti.CodicePostale)
              @Html.DisplayFor(modelItem =&gt; item.Contatti.Email)
              @Html.DisplayFor(modelItem =&gt; item.Contact.Address)
              @Html.DisplayFor(modelItem =&gt; item.Contact.CompanyId)
              @Html.DisplayFor(modelItem =&gt; item.Contact.ContactId)
              @Html.DisplayFor(modelItem =&gt; item.Company.CompanyName)
  
          &lt;/td&gt;
          &lt;td&gt;
              @Html.ActionLink("Edit", "Edit", new { id=item.ContattoID }) |
              @Html.ActionLink("Details", "Details", new { id=item.ContattoID }) |
              @Html.ActionLink("Delete", "Delete", new { id=item.ContattoID })
          &lt;/td&gt;
      &lt;/tr&gt;
  }
  
  &lt;/table&gt;
  </code></pre>
  
  <p>
  </p>