Example #02: Custom server binding

1. We must enter the name of the grid, otherwise it will not work.
[ApplyTransaction, GridAction(GridName = "Grid")]
public ActionResult CustomServerBinding(GridCommand command)
{
    // Telerik custom server binding demo: 
    // http://demos.telerik.com/aspnet-mvc/razor/grid/customserverbinding

    // We have to inform the pageSize here, since in the first call, 
    // the GridCommand is not correctly initialized, and dont forget to
    // set it in the grid too.
    command.PageSize = 15; // Please put the same pageSize as is in the view

    // We have to use another query to count because of an issue in NHibernate:
    // (currently fixed in the unreleased version 3.3.0)
    // https://nhibernate.jira.com/browse/NH-2846
    var query = NHibernateHelper.CurrentSession.Query<Product>().Fetch(x => x.Category);
    var queryToCount = NHibernateHelper.CurrentSession.Query<Product>();

    var gridQueryProvider = new GridQueryProvider(query, queryToCount);

    var gridHelper =
        new GridCustomBindingHelper<Product, ProductModel>(command, gridQueryProvider);

    var gridModel = gridHelper.BuildGridModel();

    ViewBag.Total = gridModel.Total;
    ViewBag.Data = gridModel.Data;

    return View();
}
2. Finaly, the Grid configuration
@(Html.Telerik().Grid<ProductModel>()
        .Name("Grid")
        .BindTo((IEnumerable)ViewBag.Data)
        .Columns(columns =>
        {
            columns.Bound(d => d.ProductID).Width(80);
            columns.Bound(d => d.ProductName);
            columns.Bound(d => d.QuantityPerUnit);
            columns.Bound(d => d.Category.CategoryName).Title("Category");
        })
        .DataBinding(dataBinding => dataBinding.Server()
        .Select<ExamplesController>(ctr => ctr.CustomServerBinding(null)))
        .Pageable(settings => settings.Total((int)ViewBag.Total)
        .PageSize(15)/* We have to inform the pageSize in the control too.*/)
        .EnableCustomBinding(true)
        .Sortable()
        .Filterable()
        .Groupable()

Last edited Feb 27, 2012 at 11:11 PM by Luis_Fernando, version 2

Comments

No comments yet.