title: Send Two Models into One View .Net in VB
body: <p>Edit one more time:</p>
  
  <p>So it looks like I have figured out the address part too, in the class I have:</p>
  
  <pre><code>Public ReadOnly Property addresses As IEnumerable(Of Address)
          Get
              Return _these_addresses
          End Get
      End Property
  </code></pre>
  
  <p>And in the template I have:</p>
  
  <pre><code>&lt;% For Each item In Model.addresses%&gt;
  
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;a href='&lt;%: Url.Action("Edit", "Address", New With { .pid=Model.ContactID, .id=item.AddressID }) %&gt;'&gt;
                      &lt;img src='&lt;%: Url.Content("~/Content/Images/Edit.jpg") %&gt;' alt="Edit"  /&gt;
                  &lt;/a&gt; 
                  &lt;a href='&lt;%: Url.Action("Details", "Address", New With { .pid=Model.ContactID, .id=item.AddressID }) %&gt;'&gt;
                      &lt;img src='&lt;%: Url.Content("~/Content/Images/Detail.jpg") %&gt;' alt="Details"  /&gt;
                  &lt;/a&gt; 
                  &lt;a href='&lt;%: Url.Action("Delete", "Address", New With { .pid=Model.ContactID, .id=item.AddressID }) %&gt;'&gt;
                      &lt;img src='&lt;%: Url.Content("~/Content/Images/Delete.jpg") %&gt;' alt="Delete"  /&gt;
                  &lt;/a&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;%: item.Street%&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;%: item.City%&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;%: item.StateID%&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;%: item.CountryID%&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;%: item.Zip%&gt;
              &lt;/td&gt;
           &lt;/tr&gt;
      &lt;% Next%&gt;
  
      &lt;/table&gt;
  </code></pre>
  
  <p>Edit Again:</p>
  
  <p>I added this for every field in contact to the class and it is working...I just need to figure out the list of addresses now...</p>
  
  <pre><code>Public ReadOnly Property FirstName() As String
          Get
              Return _this_contact.FirstName
          End Get
      End Property
  </code></pre>
  
  <p>E D I T:</p>
  
  <p>I about have it figured out:</p>
  
  <p>I took a shot in the dark and made a ContactViewModel based of C examples I have found in research on how to do this </p>
  
  <pre><code>Imports System
  Imports System.Collections.Generic
  Imports System.Linq
  Imports System.Web
  Imports TotallyAwesomeCRM.Contact
  Imports TotallyAwesomeCRM.Address
  
  Public Class ContactViewModel
      Public contact As Contact
      Public address As Address
      Private _this_contact As Contact
      Private _these_addresses As System.Linq.IQueryable(Of Address)
  
      Sub New(ByVal this_contact As Contact, ByVal these_addresses As System.Linq.IQueryable(Of Address))
          ' TODO: Complete member initialization -this was added by the framework for me when I tried to call this class - I don't know what to do here - resources? 
          _this_contact = this_contact
          _these_addresses = these_addresses
      End Sub
  
  End Class
  </code></pre>
  
  <p>So in my controller I: </p>
  
  <pre><code>Function Details(ByVal id As Integer) As ActionResult
          Dim this_contact = GetContact(id)
          Dim these_addresses =
              From address In addressDataContext.Addresses, xref In addressDataContext.ContactAddressesXrefs
              Where address.AddressID = xref.AddressID And xref.ContactID = id
              Select address
  
          Dim viewModel = New ContactViewModel(this_contact, these_addresses)
          Return View(viewModel)
      End Function
  </code></pre>
  
  <p>And in the template it found the contact when I started typing Model.contact</p>
  
  <p>&lt;%: Model.contact.FirstName%></p>
  
  <p>But it gave me an error there: NullReferenceException: Object reference not set to an instance of an object.</p>
  
  <p>It shouldn't be null...Please help me figure out the TODO</p>
  
  <p>=================================================================================
  O R I G I N A L  P O S T:</p>
  
  <p>This is my first .NET venture ever. Everything I have done thus far I have figured out. </p>
  
  <p>OK I have contacts, and contacts can have many addresses. I would like to display those addresses when I am viewing the detail of a Contact I would also like to have a container that is a list view of it's addresses. From that list view I want to be able to edit/delete/view the address.</p>
  
  <p>My database structure is:</p>
  
  <p>Tables:</p>
  
  <pre><code>Contacts
  &lt;contains contact info and PK&gt;
  
  Addresses
  &lt;contains address info and PK&gt;
  
  ContactAddressesXref
  ContactID
  AddressID
  </code></pre>
  
  <p>I have basically been editing the skeleton files the ASP.NET MVC empty application provides me</p>
  
  <p>So here is the last thing I have tried in my Contact controller:</p>
  
  <pre><code>'
      ' GET: /Contacts/Details/5
  
      Function Details(ByVal id As Integer) As ActionResult
          Dim this_contact =
                          From contact In dataContext.Contacts, address In addressDataContext.Addresses, xref In addressDataContext.ContactAddressesXrefs
                          Where address.AddressID = xref.AddressID And xref.ContactID = id
                          Select contact, address
          Return (View(this_contact))
      End Function
  </code></pre>
  
  <p>What I really wanted to do was query the addresses separately and send them in as their own set. I don't know if doing this is the right thing to do, but when I tried to send in 
  two models it freaked out.</p>
  
  <p>Which fails of course because the view has this in it:</p>
  
  <pre><code>&lt;%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of IEnumerable (Of TotallyAwesomeCRM.Contact))" %&gt;
  </code></pre>
  
  <p>I tried:</p>
  
  <pre><code>Inherits="System.Web.Mvc.ViewPage(Of IEnumerable (Of TotallyAwesomeCRM.Contact, Of TotallyAwesomeCRM.Address))
  </code></pre>
  
  <p>It said:</p>
  
  <pre><code> 'InitializeCulture' is not a member of 'ASP.views_contacts_details_aspx'.
  </code></pre>
  
  <p>So OK I tried:</p>
  
  <pre><code>Inherits="System.Web.Mvc.ViewPage"
  </code></pre>
  
  <p>And it throws and error here:</p>
  
  <pre><code>&lt;div class="display-field"&gt;&lt;%: Model.FirstName%&gt;&lt;/div&gt;
  </code></pre>
  
  <p>Of  course, so Am I wrong in the controller? I know I can't have just System.Web.Mvc.ViewPage(Of IEnumerable (Of TotallyAwesomeCRM.Contact)) that it will have to accept  more than that. I tried jacking with the Model.FirstName part saying Contact.FirstName, but that didn't come up in the drop down when I started writing Contact. I could do this easily in other languages, .Net seems to be a different ball game. Please help!</p>