【问题标题】:Unable to bind 2 tables to viewModel and send to view无法将 2 个表绑定到 viewModel 并发送到视图
【发布时间】:2014-02-26 23:52:36
【问题描述】:

表 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”类型。

是我遗漏了什么,还是我绑定不正确?

谢谢。

【问题讨论】:

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


    【解决方案1】:

    我想办法解决这个问题。这是更新后的 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 MerchantService) 添加到 _Services Private 变量中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-09
      • 2012-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多