asp.net-mvc 使用 viewbag 将模型传递给查看
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19843657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:18:23 来源:igfitidea点击:
pass model to view using viewbag
提问by Pnsadeghy
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'
回答by Piotr Perak
you should cast viewbag.model to List of User. Not to User.
您应该将 viewbag.model 转换为用户列表。不给用户。
@{
var Users = (List<User>)ViewBag.Users;
}

