MapleStory MapleStory - 1 month ago 9
ASP.NET (C#) Question

ASP.NET MVC :Razor <tr> do not match close tag

I have a model filled with List model and I want to use foreach loop to display it ,but ....my html looks like this:

<tr>
<td class="lottery-unit lottery-unit-0"><img src="~/images/temp/1.png"></td>
<td class="lottery-unit lottery-unit-1"><img src="~/images/temp/2.png"></td>
<td class="lottery-unit lottery-unit-2"><img src="~/images/temp/4.png"></td>
<td class="lottery-unit lottery-unit-3"><img src="~/images/temp/3.png"></td>
</tr>
<tr>
<td class="lottery-unit lottery-unit-11"><img src="~/images/temp/7.png"></td>
<td colspan="2" rowspan="2">
<a href="#"></a>
</td>
<td class="lottery-unit lottery-unit-4"><img src="~/images/temp/5.png"></td>
</tr>
<tr>
<td class="lottery-unit lottery-unit-10"><img src="~/images/temp/1.png"></td>
<td class="lottery-unit lottery-unit-5"><img src="~/images/temp/6.png"></td>
</tr>
<tr>
<td class="lottery-unit lottery-unit-9"><img src="~/images/temp/3.png"></td>
<td class="lottery-unit lottery-unit-8"><img src="~/images/temp/6.png"></td>
<td class="lottery-unit lottery-unit-7"><img src="~/images/temp/8.png"></td>
<td class="lottery-unit lottery-unit-6"><img src="~/images/temp/7.png"></td>
</tr>


lottery-unit lottery-unit-0 to lottery-unit lottery-unit-11 is eleven dto I want to display,and the tr is not fixed.
and if I use razor to write it like this :

foreach (var item in Model)
{
if (item.Order >= 0 && item.Order <= 3)
{
if (item.Order == 0)
{
<tr>

}
<td class="lottery-unit lottery-unit-0"><img src="~/images/temp/1.png"></td>
@if(item.Order == 3)
{
</tr>
}
}


the above will make some errors .then how to write the correct code ?

Answer

You need to add @ in front of the foreach and also add @: in front of the <tr> and </tr> inside the if statements. Your code should be like this:

@foreach (var item in Model)
{
    if (item.Order >= 0 && item.Order <= 3)
    {
        if (item.Order == 0)
        {
            @:<tr>
        }
        <td class="lottery-unit lottery-unit-0"><img src="~/images/temp/1.png"></td>
        if (item.Order == 3)
        {
             @:</tr>
        }
    }
}