在VB中将两个模型发送到一个视图.Net

时间:2010-07-11 20:35:11

标签: asp.net-mvc vb.net linq-to-sql viewmodel

再编辑一次:

所以看起来我已经找到了地址部分,在我的课程中:

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

在模板中我有:

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

再次编辑:

我为每个与课程联系的字段添加了这个,它正在工作......我只需要找出地址列表......

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

E D I T:

我想知道:

我在黑暗中拍摄了一个基于C示例的ContactViewModel我在研究中发现了如何做到这一点

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

所以在我的控制器中我:

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

在模板中,当我开始输入Model.contact

时,它找到了联系人

&lt;%:Model.contact.FirstName%&gt;

但它在那里给我一个错误:NullReferenceException:对象引用未设置为对象的实例。

它不应该为空......请帮我弄清楚TODO

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

这是我有史以来的第一次.NET冒险。到目前为止我所做的一切我已经想通了。

好的我有联系人,联系人可以有很多地址。我想在查看联系人的详细信息时显示这些地址我还希望有一个容器,它是其地址的列表视图。从该列表视图中,我希望能够编辑/删除/查看地址。

我的数据库结构是:

表:

Contacts
<contains contact info and PK>

Addresses
<contains address info and PK>

ContactAddressesXref
ContactID
AddressID

我基本上编辑了ASP.NET MVC空应用程序提供给我的骨架文件

所以这是我在Contact控制器中尝试过的最后一件事:

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

我真正想要做的是分别查询地址并将它们作为自己的集合发送。我不知道这样做是否正确,但是当我试图发送时 它吓坏了两个模型。

当然,这是因为视图中有这个:

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

我试过了:

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

它说:

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

好吧,我试过了:

Inherits="System.Web.Mvc.ViewPage"

它在这里引发错误:

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

当然,我在控制器中错了吗?我知道我不能只有System.Web.Mvc.ViewPage(IEnumerable(Of TotallyAwesomeCRM.Contact)),它必须接受更多。我试着使用Model.FirstName部分来说明了Contact.FirstName,但是当我开始编写Contact时,这并没有出现在下拉列表中。我可以用其他语言轻松完成这项工作,.Net似乎是一个不同的球类游戏。请帮忙!

1 个答案:

答案 0 :(得分:1)

你不需要“todo:完成成员初始化......”(除了删除它:-))。您需要更改联系人和地址的属性。

试试这个:

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
相关问题