Create N*M <table> in view by iterating through model items

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...?!

Answers


You could use HtmlHelper.Raw method to help razor


Need Your Help

Google Maps Marker Incorrect position in IE, working fine on chrome

javascript google-maps google-maps-api-3

I have plotted a route map on my page using google API. Now let say I have printed a route map from point A to Point B which goes from point C. Now I am Showing some marker with point C in the map....

How to export sqlite3 database in mac xml xls file format programatically in iphone?

iphone objective-c sqlite3 export export-to-excel

how to export the sqlite3 database table in mac xml excel .xml and microsoft excel xsl xml spreadsheets format programatically.any tutorial or sample code for that?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.