Search code examples
asp.net-mvc-3webgrid

WebGrid: Include information in the row (tr). for example the Id


I am working with the helper Html.WebGrid in MVC3, how I can do to include the data for each TR tag?.

I have data in a column, I do not want to show the column, and I need these data are on the TR of the table, and then work with JavaScript.

Thank you.

EDIT

public class MyModel
{

public DataTable Data { get; set; }

public IList<dynamic> Results()
{
    var columnas = this.Columnas();
    var result = new List<dynamic>();
    foreach (DataRow row in this.Data.Rows)
    {
        var obj = (IDictionary<string, object>)new ExpandoObject();
        foreach (DataColumn column in columns)
        {
            if (!columna.ColumnName.StartsWith("_"))
            {
                obj.Add(column.ColumnName, row[column.ColumnName]);
            }
        }
        result.Add(obj);
    }

    return result;
}

private IList<DataColumn> Columns()
{
    var columns = new List<DataColumn>();
    foreach (DataColumn column in this.Data.Columns)
    {
        columns.Add(column);
    }

    return columns;
}
}

page.schtml

@model MySite.Model.MyModel

@using System.Data

@{
    ViewBag.Title = "My page";
    Layout = "~/Views/Shared/Layout.cshtml";
}
<h2>Resultados</h2>
@{
    if (Model == null) { 
        <div>
            <div>no results</div>
        </div>
    }
    else {
        <div>
            <div>Rows:  <span>@Model.Data.Rows.Count</span></div>
            @{

        var grid = new WebGrid(source: Model.Results(), rowsPerPage: 10);

        @grid.GetHtml(
                fillEmptyRows: true,
                alternatingRowStyle: "fila-alternativa",
                headerStyle: "encabezado-grid",
                footerStyle: "pie-grid",
                mode: WebGridPagerModes.All,
                firstText: "<< Primera",
                previousText: "< Anterior",
                nextText: "Siguiente >",
                lastText: "Última >>"
            )
        }

        @Html.HiddenFor(model => Model.Filtros.TipoConsultaSiguiente)
    </div>
}
}

Solution

  • You can set up a class for this column when configure your grid

    @MvcHtmlString.Create(grid.GetHtml(htmlAttributes: new { id = "grid" },
                                             tableStyle: "members_table",
                                             rowStyle: "alt",
                                             mode: WebGridPagerModes.All,
                                             columns: grid.Columns(
                                                   grid.Column(columnName: "Id", format: @<text>@item.id</text>, style: "hiddenColumn"), ...
    

    and in hiddenColumn class set up visibility for this column.