views:

118

answers:

1

Edit one more time:

So it looks like I have figured out the address part too, in the class I have:

Public ReadOnly Property addresses As IEnumerable(Of Address)
        Get
            Return _these_addresses
        End Get
    End Property

And in the template I have:

<% For Each item In Model.addresses%>

        <tr>
            <td>
                <a href='<%: Url.Action("Edit", "Address", New With { .pid=Model.ContactID, .id=item.AddressID }) %>'>
                    <img src='<%: Url.Content("~/Content/Images/Edit.jpg") %>' alt="Edit"  />
                </a> 
                <a href='<%: Url.Action("Details", "Address", New With { .pid=Model.ContactID, .id=item.AddressID }) %>'>
                    <img src='<%: Url.Content("~/Content/Images/Detail.jpg") %>' alt="Details"  />
                </a> 
                <a href='<%: Url.Action("Delete", "Address", New With { .pid=Model.ContactID, .id=item.AddressID }) %>'>
                    <img src='<%: Url.Content("~/Content/Images/Delete.jpg") %>' alt="Delete"  />
                </a>
            </td>
            <td>
                <%: item.Street%>
            </td>
            <td>
                <%: item.City%>
            </td>
            <td>
                <%: item.StateID%>
            </td>
            <td>
                <%: item.CountryID%>
            </td>
            <td>
                <%: item.Zip%>
            </td>
         </tr>
    <% Next%>

    </table>

Edit Again:

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...

Public ReadOnly Property FirstName() As String
        Get
            Return _this_contact.FirstName
        End Get
    End Property

E D I T:

I about have it figured out:

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

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

So in my controller I:

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

And in the template it found the contact when I started typing Model.contact

But it gave me an error there: NullReferenceException: Object reference not set to an instance of an object.

It shouldn't be null...Please help me figure out the TODO

================================================================================= O R I G I N A L P O S T:

This is my first .NET venture ever. Everything I have done thus far I have figured out.

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.

My database structure is:

Tables:

Contacts
<contains contact info and PK>

Addresses
<contains address info and PK>

ContactAddressesXref
ContactID
AddressID

I have basically been editing the skeleton files the ASP.NET MVC empty application provides me

So here is the last thing I have tried in my Contact controller:

'
    ' 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

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.

Which fails of course because the view has this in it:

<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of IEnumerable (Of TotallyAwesomeCRM.Contact))" %>

I tried:

Inherits="System.Web.Mvc.ViewPage(Of IEnumerable (Of TotallyAwesomeCRM.Contact, Of TotallyAwesomeCRM.Address))

It said:

 'InitializeCulture' is not a member of 'ASP.views_contacts_details_aspx'.

So OK I tried:

Inherits="System.Web.Mvc.ViewPage"

And it throws and error here:

<div class="display-field"><%: Model.FirstName%></div>

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!

+1  A: 

You don't need "todo: complete member initialization..." (besides remove it :-)). You need to change the property for contact and address.

Try this:

Public Class ContactViewModel

    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

    Public ReadOnly Property Contact As Contact
        Get
            Return _this_contact
        End Get
    End Property

    Public ReadOnly Property Addresses As System.Linq.IQueryable(Of Address)
        Get
            Return _these_addresses
        End Get
    End Property

End Class
Tim Murphy
So I don't need to create a property for every contact field? I have the addresses working that way, but I created a property for each field in contact, it works, but it is a lot of extra coding if I don't have to.
KacieHouser
Correct. You just need a property in your viewmodel for Contact and Addresses. Then in your view you address the required property throw that. eg model.Contact.FirstName, model.Addresses(0).Country.
Tim Murphy