Example #03: One single query to count and list data

1. Here we can send our view to the client, no need to pass any data yet.
public ActionResult OneQueryToCountAndList()
{
    return View();
}
2. And then send the data through ajax requests.
[ApplyTransaction, GridAction(EnableCustomBinding = true)]
public ActionResult OneQueryToCountAndListData(GridCommand command)
{
    // The same query will be used to count and list.
    // WARN: careful with Select N+1 (I purposely left the problem in this query):
    // http://nhprof.com/Learn/Alerts/SelectNPlusOne
    var query = NHibernateHelper.CurrentSession.Query<Product>();
    //// ".Fetch(x => x.Category)" Cant be used here because of an issue in NHibernate: 
    // (currently fixed in the unreleased version 3.3.0)
    // https://nhibernate.jira.com/browse/NH-2846

    var gridHelper = new GridCustomBindingHelper<Product, ProductModel>(command, query);
            
    var gridModel = gridHelper.BuildGridModel();

    return View(gridModel);
}
3. Finaly, the Grid configuration
@(Html.Telerik().Grid<ProductModel>().Name("Grid")
        .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");
        })
        .EnableCustomBinding(true).Sortable().Filterable().Groupable()
        .Pageable(x => x.PageSize(15))
        .DataBinding(dataBinding => dataBinding.Ajax()
            .Select<ExamplesController>(ctr => ctr.OneQueryToCountAndListData(null))))

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

Comments

No comments yet.