我目前有一个控制器,该控制器可循环浏览“ Sites”数据库,并根据其站点ID获取值看起来像这样
osiTotal[s.ID] = osiPartCost[s.ID] + osiCompCost[s.ID] + osiItemCost[s.ID];
ViewBag.OSITotal[s.ID] = osiTotal[s.ID]; // Receive error message on this line
然后我的视图看起来像这样
@foreach (Site s in sites)
{
<tr>
<td style="font-weight : bold;">Total</td>
<td style="font-weight : bold;">@ViewBag.OSITotal[s.ID]</td>
</tr>
}
但是我收到错误消息
无法执行运行时绑定到空引用
我已经尝试过执行此操作
@foreach (Site s in sites)
{
<tr>
<td style="font-weight : bold;">Total</td>
<td style="font-weight : bold;">@ViewBag.OSITotal[1]</td>
</tr>
}
我在其中自动为@ViewBag.OSITotal
分配值“ 1”但仍然收到相同的错误
所以我要解决的问题是,当我试图将osiTotal [s.ID]的值分配给ViewBag时>
这是为什么?
我目前有一个控制器,该控制器在我的“站点”数据库中循环并根据其站点ID获取值。 [...
ViewBag.OSITotal是什么类型?您需要先创建一个新数组或集合并为其分配一个新数组或集合,否则它将为null。 ViewBag.OSITotal [0]尝试取消引用提供错误的null集合。例如,如果ID为int: