Telerik MVC Grid, Truly Ajax custom binding

iLemming picture iLemming · Aug 3, 2011 · Viewed 7.1k times · Source

I'm using grid that looks like that:

 Html.Telerik().Grid(Model).Name("preciousGrid").
     ... bla bla bla..
     .ClientEvents(events => events.OnDataBinding("onDataBinding"))
     .Columns(columns =>
         {
           columns.Bound(o => o.Date);
           columns.Bound(o => o.Name);

Yes, I'm totally ignoring .DataBinding stuff to use custom ajax call. Why? I need to send to the server more data, rather than a simple id. And the only way to gather that data is by traversing DOM elements. So, none of the methods suggested by Telerik wouldn't work in my case.

Everything works - in onDataBinding, after the necessary data gathered and sent to the server, server returns results, grid displays that data.

But still there is a problem. Paging doesn't work. And the footer shows something like that:

enter image description here

Any ideas?

UPD: Oh... maybe I should send to the server paging info and return results based on that? How to do that? Can you show me a sample?

UPD2: GridCommand command doesn't send Paging info to the server by default(if I omit it in $.ajax and still put a GridCommand parameter in the action method it would send something to the controller, but PageSize is always equals 10 (default value), and Page is always 1. So I guess I have to hardcodedly include these values in $.ajax. But I don't know how can I get PageSize and Page values on the client?

Answer

Brett picture Brett · Aug 3, 2011

If you're doing custom data binding, you must handle the paging and sorting yourself, something like that shown below. The example on the demo site is pretty reasonable...

[GridAction(EnableCustomBinding = true)]
public ActionResult _Index(GridCommand command)
{
    IEnumerable<MyObject> data = GetData(command.Page - 1, command.PageSize);
    int count = GetDataCount();
    return View(new GridModel { Data = data, Total = count });
}

in the view you have:

.DataBinding(dataBinding => dataBinding.Ajax().Select("_Index", "Subjects"))
.Pageable(p =>
{
    p.PageSize(Model.PageSize, Model.PageSizes);
    p.Style(GridPagerStyles.PageInput | GridPagerStyles.NextPreviousAndDropDown);
})