我在视图中有以下代码:
@{
var i = 0;
var posts = Session["num"] as List<class_project.Models.Post>;
if (posts != null)
{
foreach (object item in posts)
{
<p>@item</p>
}
}
}
因此我得到了:几个class_project.Models.Post字符串。
如何显示实际物体? 我尝试使用item.something,但这并没有真正起作用。
答案 0 :(得分:6)
尝试在foreach上使用对象类型“class_project.Models.Post”或“var” 和模板上的属性:
@{
//var i = 0; //You can delete this variable.
var posts = Session["num"] as List<class_project.Models.Post>;
if (posts != null)
{
//foreach (class_project.Models.Post item in posts)
//the same as "foreach (var item in posts)"
foreach (var item in posts)
{
<p>@item.PropertyOne</p>
<p>@item.PropertyTwo</p>
}
}
}