我似乎無法弄清楚這一點,我對MVC模型並不陌生,但也許我的大腦只是疲憊不堪。Linq從列表中選擇
視圖模型
Public Class CategoriesViewModel
Public Property Categories As List(Of cihCategoryOrgDef)
Public Property Category As cihCategoryOrgDef
Public Property SelectedItem As String
Public Sub New(organizationId As Guid, codId As Guid)
Categories = lists.organizationClubCategories
Category = From x In Categories
Where x.codId = codId
Select x
End Sub
End Class
CategoriesController
Function Edit(codId As Guid) As ActionResult
Dim model As CategoriesViewModel = New CategoriesViewModel(ZenCommon.CurrentOrgId, codId)
Return View(model)
End Function
當我運行它,我得到一個 「無效強制轉換異常」 的Category = From x....
線
Unable to cast object of type 'WhereSelectListIterator`2[Library.cihCategoryOrgDef,Library.cihCategoryOrgDef]' to type 'Library.cihCategoryOrgDef'.
我試圖在我的View中使用一個Category
。所以我可以爲該特定類別編輯一個頁面。我是否全部錯了?