我尝试添加一些我想在 ASP.NET MVC 3 视图中使用的虚拟记录,以为一些实验提供数据。我试试这个:
var dummyData = new[]
{
new {Row = 1, Col = 1, IsRequired = true, QuestionText = "Yes?", FieldValue = "int"},
new {Row = 1, Col = 2, IsRequired = true, QuestionText = "Yes?", FieldValue = "int"},
new {Row = 2, Col = 1, IsRequired = true, QuestionText = "No?", FieldValue = "string"},
new {Row = 3, Col = 1, IsRequired = false, QuestionText = "No?", FieldValue = "string"}
}.ToList();
ViewBag.Header = dummyData;
但是,当我尝试在我的视图中使用数据时:
@{
foreach (var item in ViewBag.Header)
{
<tr><td>@item.QuestionText</td><td>@item.FieldValue</td></tr>
}
}
我得到这个错误 - 'object' does not contain a definition for 'QuestionText'
。我认为我创建列表的方式有问题,但不是 100% 确定。