在视图中显示项目列表时出现问题。无法将它从ItemStore.Item转换为IEnumerable,我还将“@Method List(Of ItemStore.Item)”添加到list.vbhtml并且它无法显示传入字典的项目是类型' Item Store.Item',但是这个字典需要一个'System.Collections.Generic.List`1 [Item Store.Item]'类型的模型项。
的 的 **** CatalogueController 的 * ** * ** * ** *
Function About() As ActionResult
Return View()
End Function
Function Index() As String
Return "Index"
End Function
Function List(search As String) As ActionResult
Dim model As New SQLItemModel
Dim items = model.SelectById(search)
Return View(items)
End Function
的 的 ** * ** * List.vbhtml 的 * ** * ** * ****
The search text is @ViewBag.Message
@For Each item In Model
@item.ID
的 的 ** * ** * *** SQLItemModel.vb 的 * ** * ** * ** * * < /强>
Private connectionString As String = My.Settings.sqlconnection
Public Function SelectAll() As ICollection(Of Item)
Dim items As New List(Of Item)
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim cmd As New SqlCommand("select * from item", connection)
Dim reader As SqlDataReader = cmd.ExecuteReader()
While reader.Read()
items.Add(GetItem(reader))
End While
End Using
Return items
End Function
Public Function SelectById(ItemID As Integer) As Item
Dim item As Item = Nothing
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim cmd As New SqlCommand()
cmd.CommandText = "select * from Tbl_Items where ID = @ID"
cmd.Connection = connection
cmd.Parameters.Add(New SqlParameter("ID", ItemID))
Dim reader As IDataReader = cmd.ExecuteReader()
If reader.Read() Then
Item = GetItem(reader)
End If
End Using
Return Item
End Function
Private Function GetItem(reader As SqlDataReader) As Item
Dim ID = reader("ID")
Dim ProductCode = reader("ProductCode")
Dim Brand = reader("Brand")
Dim Description = reader("Description")
Dim Colour = reader("Colour")
Dim Finish = reader("Finish")
Dim Type = reader("Type")
Dim Size = reader("Size")
Dim Unit = reader("Unit")
Return New Item(ID, ProductCode, Brand, Description, Colour, Finish, Type, Size, Unit)
End Function
答案 0 :(得分:1)
SelectById
只返回一个Item
(而SelectAll
会返回Item
s的集合,但您的列表视图需要一个集合。
一个简单的解决方法是将单个项目包装在一个数组中:
Function List(search As String) As ActionResult
Dim model As New SQLItemModel
Dim items = model.SelectById(search)
Return View({items})
End Function