pass model to view using viewbag
Asked Answered
C

1

6

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'
Catalectic answered 7/11, 2013 at 18:20 Comment(2)
Looks like there are two major problems with your code. Like @Peri mentioned, you're not casting to a container of Users. Also I think @Html.DisplayFor(item.username) will throw an error since you're not using an expression like item => item.username so I suggest you use @Html.Display(item.username) instead =]Wychelm
i think problem is in @foreach (var item in Users) or in var Users = (Documentation.Models.User)ViewBag.Users;Catalectic
D
11

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

@{
    var Users = (List<User>)ViewBag.Users;
}
Doviedow answered 7/11, 2013 at 18:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.