pass model to view using viewbag

asp.net-mvc entity-framework viewbag

Question

I would like to send two models, possibly for another page and I need more.

controller

ViewBag.Users = db.Users.ToList();

with View

@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>

I know my code is incorrect, yet I get no response.

I received this error

 foreach statement cannot operate on variables of type 'Documentation.Models.User' because 'Documentation.Models.User' does not contain a public definition for 'GetEnumerator'
1
6
12/17/2013 8:44:18 AM

Accepted Answer

I recommend casting viewbag. model to a User List. Don't use.

@{
    var Users = (List<User>)ViewBag.Users;
}
11
11/7/2013 7:12:48 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow