我希望将连接的lINQ表达式中两个表的所有列发送到我的视图中。它基本上是一个客户记录和客户地址记录。它是用VB编写的,但是C#答案会做得很好。
我不确定我的方法是否正确,并且无法将结果转换为视图所期望的结果。
我的结果类被定义为
Partial Public Class CustomerContext
Private m_customer As List(Of Customer)
Public Property cust As List(Of Customer)
Get
cust = m_customer
End Get
Set(value As List(Of Customer))
m_customer = value
End Set
End Property
Private m_address As List(Of CustomerAddress)
Public Property addr As List(Of CustomerAddress)
Get
addr = m_address
End Get
Set(value As List(Of CustomerAddress))
m_address = value
End Set
End Property
End Class我的控制器定义如下: Function Details(可选的ByVal id as Integer = Nothing) As ActionResult
Dim cc = (From c In db.Customers Join a In db.CustomerAddresses On c.CustomerID Equals a.AddressID
Where (c.CustomerID = id)
Select New CustomerContext With {.addr = db.CustomerAddresses.ToList, .cust = db.Customers.ToList})
If IsNothing(cc) Then
Return HttpNotFound()
End If
Return View(cc)
End Function我的观点是
@模型类型MvCAdventureWorkTest.CustomerContext
<table>
<tr class="displayline">
<td>
<table>
<tr>
<td>
@Html.DisplayNameFor(Function(model) model.cust.Item("Name"))
</td>
<td class="displaydatahalf">
@Html.DisplayFor(Function(model) model.cust.item("Name"))
</td>
</tr>
<tr>
<td >
@Html.DisplayNameFor(Function(model) model.addr.item("address"))
</td>
<td>
@Html.DisplayFor(Function(model) model.addr.item("address"))
</td>
</tr>
我得到的错误信息是
传入字典的模型项的类型为'System.Data.Objects.ObjectQuery`1MvCAdventureWorkTest.CustomerContext',,但此字典需要类型为“MvCAdventureWorkTest.CustomerContext”的模型项。
发布于 2014-02-20 02:43:06
我的结论是,您不能或不能轻松地将类型为iqueryable的对象转换为可用的类型,无论是ienumerable或list( of ),但工作原理如下:
Dim cc As新CustomerContext cc.cust = (From c In db.Customers Where (c.CustomerID = id) Select c).ToList cc.addr = (From a In db.CustomerAddresses Where (a.CustomerID = id) Select a).ToList()
返回视图(Cc)
https://stackoverflow.com/questions/21787332
复制相似问题