传递模型以使用viewbag查看

Pns*_*ghy 6 asp.net-mvc entity-framework viewbag

我想在我的视图中发送两个模型,在其他页面中可能需要更多

调节器

ViewBag.Users = db.Users.ToList();
Run Code Online (Sandbox Code Playgroud)

并在视图中

@using Documentation.Models

@{
    var Users = (Documentation.Models.User)ViewBag.Users;
}

<table>
@foreach (var item in Users) {
    <tr>
        <td>
            @Html.DisplayFor(item.username)
        </td>
    </tr>
}
</table>
Run Code Online (Sandbox Code Playgroud)

但我得不到答案,我知道我的代码不对

我收到了这个错误

 foreach statement cannot operate on variables of type 'Documentation.Models.User' because 'Documentation.Models.User' does not contain a public definition for 'GetEnumerator'
Run Code Online (Sandbox Code Playgroud)

Pio*_*rak 11

你应该将viewbag.model强制转换为用户列表.不是用户.

@{
    var Users = (List<User>)ViewBag.Users;
}
Run Code Online (Sandbox Code Playgroud)