【发布时间】:2020-03-14 19:14:37
【问题描述】:
您好,我有一个包含 5 个站点的“站点”表。现在它在数组中循环,但model.offReport 部分不断被列表中的以下站点覆盖。为了防止这种情况,在循环结束时,我希望它将新数据添加到数组中,这样当我返回它时,它将返回所有站点的数据,而不仅仅是数组中的最后一个站点。
目前我有一个看起来像这样的控制器方法
foreach (Site s in sites)
{
foreach (OffSiteItemDetails d in s.ItemDetails)
{
ViewBag.OffReportColumns += new List<List<string>>()
{
s.Name,
"",
"",
"Average Cost",
"",
"",
"Average Cost (With labour)"
};
ViewBag.OffReportRows = new List<List<string>>()
{
new List<string>()
{
"Parts",
"",
"",
osiPartCost[s.ID].ToString("C2"),
"",
"",
osiPartCost[s.ID].ToString("C2")
},
new List<string>()
{
"",
"600000 series",
osiOpc6[s.ID].ToString("C2"),
"",
"600000 series",
osiOpc6[s.ID].ToString("C2"),
""
},
new List<string>()
{
"",
"700000 series",
osiOpc7[s.ID].ToString("C2"),
"",
"700000 series",
osiOpc7[s.ID].ToString("C2"),
""
},
new List<string>()
{
"",
"800000 series",
osiOpc8[s.ID].ToString("C2"),
"",
"800000 series",
osiOpc8[s.ID].ToString("C2"),
""
},
new List<string>()
{
"Tools",
"",
"",
osiTools[s.ID].ToString("C2"),
"",
"",
osiTools[s.ID].ToString("C2"),
},
new List<string>()
{
"",
"900000 series",
osiOpc9[s.ID].ToString("C2"),
"",
"900000 series",
osiOpc9[s.ID].ToString("C2"),
""
},
new List<string>()
{
"",
"Other",
osiOpco[s.ID].ToString("C2"),
"",
"Other",
osiOpco[s.ID].ToString("C2"),
""
},
new List<string>()
{
"Components",
"",
"",
osiCompCost[s.ID].ToString("C2"),
"",
"",
osiLoCompCost[s.ID].ToString("C2")
},
new List<string>()
{
"Items",
"",
"",
osiItemCost[s.ID].ToString("C2"),
"",
"",
osiLoItemCost[s.ID].ToString("C2")
},
};
ViewBag.OffReporTotal = new List<List<string>>()
{
new List<string>()
{
"Total",
"",
"",
osiTotal[s.ID].ToString("C2"),
"",
"",
osiFltotal[s.ID].ToString("C2")
},
};
}
}
ViewBag.osiGrandTotal = new List<List<string>>()
{
new List<string>()
{
"OSI Grand Total",
"",
"",
ostotal.ToString("C2"),
"",
"",
ofltotal.ToString("C2")
},
};
return model;
}
}
}
这是我的看法
@foreach (Site s in sites)
{
<tr style="color:black">
@foreach (var col in ViewBag.OffReportColumns)
{
<th>@col </th>
}
</tr>
@foreach (var row in ViewBag.OffReportRows)
{
<tr style="color:black">
@foreach (var cell in row)
{
<td>@cell</td>
}
</tr>
}
@foreach (var row in ViewBag.OffReporTotal)
{
<tr style="font-size: 20px">
@foreach (var cell in row)
{
<td>@cell</td>
}
</tr>
}
@foreach (var row in ViewBag.osiGrandTotal)
{
<tr style="font-size: 20px">
@foreach (var cell in row)
{
<td>@cell</td>
}
</tr>
}
对我如何做这件事有什么建议吗?
【问题讨论】:
标签: c# arrays asp.net-mvc viewbag