首页 > 解决方案 > 是否可以在 MVC 中为 ViewBag 分配 ID?

问题描述

您好,我正在尝试为 ViewBag 分配 ID,但不确定是否可行。

我有一个循环遍历我的站点数据库的 for 循环。

float[] osiTotal = new float[sites.Count + 1];
foreach (Site s in sites)
{  
       osiTotal[s.ID] = osiPartCost[s.ID] + osiCompCost[s.ID] + osiItemCost[s.ID];
       ViewBag.OSITotal[s.ID] = osiTotal[s.ID];
}

然后在视图页面上看起来像这样

foreach (Site s in sites)
{ 
    <tr>
          <td>Total</td>
          <td></td>
          <td></td>
          <td>@ViewBag.OffReportTotal[s.ID]</td>
    </tr>
}

但这给了我“无法对可空引用执行运行时绑定”错误消息,所以我想知道我想要做的事情是否可行?

标签: c#asp.net-mvclinqviewbag

解决方案


推荐阅读