我正在尝试显示会员/角色表中的“角色”列表
我的控制器是:
' GET: /Admin/Index
Function Index() As ActionResult
Dim model = New AdminRoles()
model.Roles = Roles.GetAllRoles()
Return View(model)
End Function
我的模特是:
Imports System.Data.Entity
Imports System.ComponentModel.DataAnnotations
Imports System.ComponentModel
Public Class AdminRoles
Public Property Roles() As String()
End Class
Public Class AdminRolesDBContext
Inherits DbContext
Public Property AdminRole() As DbSet(Of AdminRoles)
End Class
我的观点是:
@ModelType IEnumerable(Of MPConversion.AdminRoles)
@Code
ViewData("Title") = "Index"
End Code
<h2>Index</h2>
<p> @Html.ActionLink("Create New", "Create")</p>
<table><tr><th>Role</th><th></th></tr>
@For Each item In Model
Dim currentItem = item
@<tr>
<td>currentitem.Roles</td>
<td>
@*@Html.ActionLink("Edit", "Edit", New With {.id = currentItem.PrimaryKey}) |
@Html.ActionLink("Details", "Details", New With {.id = currentItem.PrimaryKey}) |
@Html.ActionLink("Delete", "Delete", New With {.id = currentItem.PrimaryKey})*@
</td>
</tr>
Next
</table>
但是我得到了错误:
传递到字典中的模型项的类型为'MPConversion.AdminRoles',但此字典需要类型为'System.Collections.Generic.IEnumerable`1 [MPConversion.AdminRoles]'的模型项。
谁能看到我出错的地方?
答案 0 :(得分:1)
您的类adminRoles没有实现IEnumerable。
答案 1 :(得分:1)
只需使用
@ModelType MPConversion.AdminRoles
然后
@For Each item In Model.Roles
您的AdminRoles类包含Roles.GetAllRoles()返回的某种集合,但对象本身(AdminRoles)不是集合,并且根本不实现IEnumerable。
<强>更新强> 优化一点:
Cotroller
Function Index() As ActionResult
IEnumerable(Of String) allRoles = Roles.GetAllRoles() // modify the GetAllRoles method
Return View(allRoles)
End Function
查看:
@ModelType IEnumerable(Of String)
@Code
ViewData("Title") = "Index"
End Code
<h2>Index</h2>
<p> @Html.ActionLink("Create New", "Create")</p>
<table><tr><th>Role</th><th></th></tr>
@For Each item In Model
<tr>
<td>@item</td>
@* Commented part deleted for brevity *@
</tr>
Next
</table>