1

我正在尝试显示成员/角色表中的“角色”列表

我的控制器是:

' 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]”的模型项。

谁能看到我哪里出错了?

4

2 回答 2

1

您的类 adminRoles 没有实现 IEnumerable。

于 2012-05-04T14:08:33.247 回答
1

只需使用

@ModelType MPConversion.AdminRoles

进而

 @For Each item In Model.Roles

您的 AdminRoles 类包含某种由 Roles.GetAllRoles() 返回的集合,但对象本身 (AdminRoles) 不是一个集合,并且根本不实现 IEnumerable。

更新 以优化一点:

控制器

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>
于 2012-05-04T14:15:33.570 回答