Question

Well, easily I have a model:

@model IEnumerable<CompanyWebSite.Category>

And it's quite straightforward to iterating through its items and create a One-item-per-row table to show them.

And the problem is that I want to create a table like this:

<table>
    <tr>
        <td></td>
        <td></td>
    </tr>
    //
    // ...
    //
    <tr>
        <td colspan="2"> ... LastItem ...</td>
    </tr>
</table>

Seems simple, but I can't do that...!

@{
ViewBag.Title = "Products";
var last = Model.Last();
int i = 1;
}

<table id="tblCats">
@foreach(var item in Model)
{                        
    if (i == 1){
        <tr>
        @(i = 2)
    }

        @if (!item.Equals(last))
        {                                            
            <td>
                <a href="#"><h4>@item.CategoryName</h4></a>
                <label>@item.Description</label>
            </td>   
            if (i == 2)
            {
                </tr>
                i = 1;
            }
            else
                i = 2;         
        }
        else
        {
            <td colspan="2">

            </td>
            </tr>
        }                                                                                                  
}

Because of the first opening <tr>, Razor gets confused and doesn't see the closing }s and else statements... What can I do...?!

Was it helpful?

Solution

You could use HtmlHelper.Raw method to help razor

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top