Reputation: 7818
I'm trying to show a list of "roles" from the membership/role tables
My controller is:
' GET: /Admin/Index
Function Index() As ActionResult
Dim model = New AdminRoles()
model.Roles = Roles.GetAllRoles()
Return View(model)
End Function
My model is:
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
My view is:
@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>
However I get ther error:
The model item passed into the dictionary is of type 'MPConversion.AdminRoles', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MPConversion.AdminRoles]'.
Can anyone see where I'm going wrong?
Upvotes: 1
Views: 2290
Reputation: 2583
simply use
@ModelType MPConversion.AdminRoles
and then
@For Each item In Model.Roles
Your AdminRoles class contain some kind of collection returned by Roles.GetAllRoles(), but the object itself (AdminRoles) is not a collection, and doesn't implement IEnumerable at all.
Updated To optimize a bit:
Cotroller
Function Index() As ActionResult
IEnumerable(Of String) allRoles = Roles.GetAllRoles() // modify the GetAllRoles method
Return View(allRoles)
End Function
View:
@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>
Upvotes: 1