Xyzk Xyzk -4 years ago 69
ASP.NET (C#) Question

WebGrid.table does not appear/generate html

Problem:
I'm writing a modification to an existing application, basing on another existing modification. In the original, a table is generated based on input data using WebGrid. I'm trying to also create a table, but using slightly different method. For some reason however, for the same data, my code does not generate a table (as in, I do not even have a

<table></table>
in the page code.
Code:

@{
List<WebGridColumn> columns = new List<WebGridColumn>();
// id column
WebGridColumn idColumn = new WebGridColumn();
idColumn.ColumnName = "ID";
idColumn.Style = "";
idColumn.Format = @<text><span>@item.ID</span></text>;
columns.Add(idColumn);

grid.Table(tableStyle: "span12",
rowStyle: "",
columns: columns,
displayHeader: true,
htmlAttributes: new { id = "tableId", style = "margin-bottom: 15px;" }
);
}


What I tried:
I tried reading documentation and finding other examples, but nearly all of them define the whole table at once, with all columns descriptions inside the table description, so I'm wondering if that's the place that causes problems

Answer Source

Welp, it appears the problem was in the fact that the grid.table command was in between the '@{ }'. It should have been outside, and then called with @grid.table by itself. No idea why, but it works. Corrected code:

@{
            List<WebGridColumn> columns = new List<WebGridColumn>();
            // id column
            WebGridColumn idColumn = new WebGridColumn();
            idColumn.ColumnName = "ID";
            idColumn.Style = "";
            idColumn.Format = @<text><span>@item.ID</span></text>;
            columns.Add(idColumn);
}
            @grid.Table(tableStyle: "span12",
                rowStyle: "",
                columns: columns,
                displayHeader: true,
                htmlAttributes: new { id = "tableId", style = "margin-bottom: 15px;" }
                );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download