无法将2个表绑定到viewModel并发送到视图

时间:2014-02-26 23:52:37

标签: vb.net entity-framework model-view-controller data-binding viewmodel

表1. MerchantBusiness 表2. MerchantServices

我想在一个视图中显示两个表数据。

我想在视图上显示MerchantBusiness详细信息。非常基本的商业名称,地址等。

MerchantServices是企业拥有的服务列表。

所以我首先创建了viewModel

Public Class MerchantServicesModel

Public Property MerchantViewModel() As Merchant
    Get
        Return _Merchant
    End Get
    Set(value As Merchant)
        _Merchant = value
    End Set
End Property
Private _Merchant As Merchant



Public Property ServiceViewModel() As IEnumerable(Of MerchantService)

    Get
        Return _Services
    End Get
    Set(value As IEnumerable(Of MerchantService))
        _Services = value
    End Set
End Property
Private _Services As MerchantService

End Class

并在我的控制器中将两个表绑定到viewModel

Function Index(id As Integer) As ActionResult
    Dim db As New MerchantEntities

    Dim MerchantDetails As New MerchantServicesModel
    MerchantDetails.MerchantViewModel = db.Merchants.Find(id)

    MerchantDetails.ServiceViewModel = (From m In db.MerchantServices
                           Where m.MerchantID = id
                           Select m).ToList()


    Return View(MerchantDetails)


End Function


My View

@ModelType MerchantServicesModel 

@Code

Layout = "~/Views/Shared/_Layout.vbhtml"

End Code


@Html.DisplayFor(Function(model) model.MerchantViewModel.BusinessName)

@For Each item In Model.ServiceViewModel

@Html.DisplayFor(Function(model) item.ServiceName)

Next

我认为MerchantBusiness并不是完整列表,只是特定业务的详细信息 但MerchantServices是一个IEnumerable列表,所以我创建了一个表格和风格等等等等。

现在错误即将到来

其他信息:无法将“System.Collections.Generic.List`1 [VBClassMVC.MerchantService]”类型的对象强制转换为“VBClassMVC.MerchantService”。

我错过了什么,或者我绑得不正确?

谢谢。

1 个答案:

答案 0 :(得分:0)

干草,我设法解决了这个问题。这是更新的ViewModel

 Public Property ServiceViewModel() As IEnumerable(Of MerchantService)

    Get
        Return _Services
    End Get
    Set(value As IEnumerable(Of MerchantService))
        _Services = value
    End Set
End Property
Private _Services As IEnumerable(Of MerchantService)

我需要将IEnumerable(Of Merchant Service)添加到_Services Private变量..