Merge multiple `<td>`'s in one `<tr>`. ASP.NET MVC3
I have an ASP.NET MVC3 project.
I want to insert some <td>
's in one table <tr>
. These <td>
's contain one <div>
each. These comes from a switch() which is in a partialView and renders into another View (where is the <tr>
which could contain multiple <td>
's).
This is the partialView:
@foreach (var item in Model)
{
switch (item.PitlaneId)
{
case 1:
<td id=a>
<div class="drag">
Name: @Html.DisplayFor(modelItem => item.Name) <br />
Constrainted: @Html.DisplayFor(modelItem => item.Constrainted) <br />
</div>
</td>
break;
case 2:
<td id=b>
<div class="drag">
Name: @Html.DisplayFor(modelItem => item.Name) <br />
Constrainted: @Html.DisplayFor(modelItem => item.Constrainted) <br />
</div>
</td>
break;
case 3:
<td id=c>
<div class="drag">
Name: @Html.DisplayFor(modelItem => item.Name) <br />
Constrainted: @Html.DisplayFor(modelItem => item.Constrainted) <br />
开发者_如何转开发 </div>
</td>
break;
default: break;
}
}
And this partialView renders here:
<table title="test" id="testTableID">
<colgroup> @foreach (var item in Model){<col width="300" />} </colgroup>
<tbody>
<tr>
@{Html.RenderAction("Tasks"); } @* <--- Here it renders*@
</tr>
</tbody>
</table>
The PROBLEM appears when switch() goes to the same case multiple times. If I have PitlaneId in this order: 1,2,2,3; It will create 4 <td>
's in my table. In this case I should have only 3 <td>
's and the two <div>
's with the same PitlaneId=2 should be inside the same <td>
. I mean <td>
's with the same id should merge together. Same situation when PitlaneId comes in this order: 1,2,3,2.
This is the only way I could make it work. It seems smelly, but it provides the order of elements you specified.
@Html.Raw("<table><tr><th>ID 1</th><th>ID 2</th><th>ID 3</th></tr><tr>")
@foreach (int id in Model.OrderBy(p => p.PitlaneID)
.Select(p => p.PitlaneID).Distinct())
{
switch(id)
{
case 1:
@:<td id="a">
break;
case 2:
@:<td id="b">
break;
case 3:
@:<td id="c">
break;
default: break;
}
foreach (var item in Model.Where(p => p.ID == id))
{
<div class="drag">
Name: @Html.DisplayFor(modelItem => item.Name) <br />
Constrainted: @Html.DisplayFor(modelItem => item.Constrainted) <br />
</div>
}
Html.Raw("</td>");
}
@Html.Raw("</tr></table>")
精彩评论