Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass model to view using viewbag

i want to send two model in my view and in other page maybe and need more

controller

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

and in 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>

but i get no answer , i know my code is not right

i got 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'
like image 839
Pnsadeghy Avatar asked Nov 07 '13 18:11

Pnsadeghy


1 Answers

you should cast viewbag.model to List of User. Not to User.

@{
    var Users = (List<User>)ViewBag.Users;
}
like image 185
Piotr Perak Avatar answered Sep 20 '22 12:09

Piotr Perak