您好我想使用循环概念
生成这样的视图页面No CType PNum
1 Cap 12
2 Bottle 23
这是我的查看页面
@for (int i = 1; i < (Enumerable.Count(@ViewBag.TestComponent_ComponentType)); i++)
{
foreach (string component_type in ViewBag.TestComponent_ComponentType)
{
foreach (string part_number in ViewBag.TestComponent_ComponentPNumb)
{
<td>@i) </td>
<td> @Html.Raw(component_type)</td>
<td>(Part #:@Html.Raw(part_number))</td>
i = i + 1;
}
}
}
这是我的控制器
ViewBag.TestComponent_ComponentType = context.Test_Component.Where(x => x.TestRequestId == id).Select(x => x.ComponentType).ToList();
ViewBag.TestComponent_ComponentPNumb = context.Test_Component.Where(x => x.TestRequestId == id).Select(x => x.PartNumber).ToList();
请帮帮我。
答案 0 :(得分:1)
首先,由于两个集合都具有来自同一对象属性的值,因此您不必构建两个集合。 我建议你试试这个: 在您的控制器中:
ViewBag.ComponentData = context.Test_Component.Where(x => x.TestRequestId == id).ToList();
在您看来:
@{
List<Test_Component> compList = ViewBag.ComponentData;
int count = 1;
}
<table>
<tr>
<th>No</th>
<th>cType</th>
<th>pNum</th>
</tr>
@foreach(Test_Component t in compList)
{
<tr>
<td>@count</td>
<td>@t.ComponentType</td>
<td>@t.PNum</td>
</tr>
count++;
}
</table>