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 に答える