【发布时间】:2017-07-11 12:49:39
【问题描述】:
这是我的代码示例,我在其中显示有关我的客户的信息:
<table class="table table-striped jambo_table bulk_action">
<thead>
<tr class="headings">
<th class="column-title">Address </th>
<th class="column-title">Title</th>
</tr>
</thead>
<tbody>
@foreach (var customer in ViewBag.Customers)
{
<tr>
<td>@customer.Code</td>
<td>@customer.DefaultName.Name</td>
</tr>
}
</tbody>
</table>
正如你们所看到的,我正在循环访问ViewBag 以在我的表格中显示一些数据,
我在ViewBag.Customer中没有数据的情况下测试了它,它没有抛出任何异常,
foreach 怎么没有在空列表上抛出异常?
我想我会得到类似的东西:
对象引用未设置为对象的实例
如果我在没有数据的情况下尝试遍历ViewBag.Customer..
怎么会?
【问题讨论】:
-
向我们展示你的控制器类。全班同学。
-
如果它是一个空列表,它不会抛出错误。如果它是 null 应该有一个错误(我认为)。
-
为什么不使用 ViewModel 将所有属性传递给强类型视图?
标签: c# asp.net asp.net-mvc views