This is a migrated thread and some comments may be shown as answers.

[Solved] Server-side paging/sorting WITHOUT Ajax

2 Answers 167 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
John
Top achievements
Rank 1
John asked on 09 Mar 2011, 06:09 PM
Hi,

So like the title says, I'm trying to implement server side paging and sorting without an ajax call. Code follows:

View

@model DataObjects.TestEntryPageWrapper

@(Html.Telerik().Grid(Model.TestEntries)
        .Name("Grid")
        .DataBinding(dataBinding => dataBinding.Server()
            .Select("Index","Notification"))
        .Pageable(settings =>
        {
            settings.Enabled(true);
            settings.Total(Model.TotalRecords);
            settings.PageTo(Model.Page);
            settings.PageSize(Model.PageSize);
            settings.Position(GridPagerPosition.Bottom);
        })
        .Columns(columns =>
        {
            columns.Bound(o => o.TesterName).Title("Tester");
            columns.Bound(o => o.InstitutionID).Title("Inst");
            columns.Bound(o => o.IPID).Title("IPID");
            columns.Bound(o => o.Category).Title("Category");
            columns.Bound(o => o.Description).Title("Description");
            columns.Bound(o => o.DateCreated).Title("Date Created").Format("{0:g}");
            columns.Bound(o => o.ShortResultText).Title("Result");
            columns.Template(
                @<text>
@Html.ActionLink("Load", "Send", new { id = item.ID })
|
@Html.ActionLink("Details", "Details", new { id = item.ID })
|
@Html.ActionLink("Delete", "Delete", new { id = item.ID })
</text>
            ).Title("Commands").HtmlAttributes(new { @style = "white-space: nowrap; color: #abc;" });
        })
        .Scrollable(scrolling => scrolling.Enabled(false))
        .Sortable(sorting => sorting.Enabled(true))
        .Filterable(filtering => filtering.Enabled(true))
        .Groupable(grouping => grouping.Enabled(true))
        .Footer(true)    
 
)

controller:

[GridAction(EnableCustomBinding = true, GridName="Grid")]
        public ActionResult Index(string filterValue)
        {
            int gridPage;
            int.TryParse(Request["Grid-Page"], out gridPage);
            if (gridPage == 0)
                gridPage = 1;
 
            int pageSize;
            int.TryParse(Request["Size"], out pageSize);
            if (pageSize == 0)
                pageSize = 20;
 
            string orderBy = "DateCreated";
            if (!string.IsNullOrEmpty(Request["Grid-orderby"]))
                orderBy = Request["Grid-orderby"];
 
            //ViewData["FilterValue"] = filterValue;
 
            var pageWrapper = new TestEntryPageWrapper() {Page = gridPage, PageSize = pageSize};
            using (var db = new STTDataContext())
            {
                pageWrapper.TotalRecords = db.TestEntries.Count();
                int startIndex = (gridPage - 1) * pageSize;
 
                pageWrapper.TestEntries = db.TestEntries.Skip(startIndex).Take(pageSize).ToList();
                return View(pageWrapper);
            }
        }

When my grid loads, it only has one page available when there should be 3. Any help would be greatly appreciated.

John

2 Answers, 1 is accepted

Sort by
0
Merritt
Top achievements
Rank 1
answered on 11 Mar 2011, 09:24 PM
Add: GridCommand command to your index action parameter.

Then in it, you'll need to access the values, something like so:

foreach (SortDescriptor sortDescriptor in command.SortDescriptors)
{
    if (sortDescriptor.SortDirection == ListSortDirection.Ascending)
    {
        switch (sortDescriptor.Member)
        {
            case "IP":
                data = _unitOfWork.Clients.OrderBy(x => x.IP);
                break;
            case "TerminalNumber":
                data = _unitOfWork.Clients.OrderBy(x => x.Terminal.Number);
                break;
        }
    }
    else
    {
        switch (sortDescriptor.Member)
        {
            case "IP":
                data = _unitOfWork.Clients.OrderByDescending(x => x.IP);
                break;
            case "TerminalNumber":
                data = _unitOfWork.Clients.OrderByDescending(x => x.Terminal.Number);
                break;
        }
    }
}
 
if (data == null)
{
    data = _unitOfWork.Clients.OrderBy(x => x.IP);
}
 
if (command.PageSize > 0)
{
    data = data.Skip((command.Page - 1) * command.PageSize);
}

This will work for built-in functionality anytime you call a command, like Select (eg done when clicking on a sortable column header). However, your template contains action links and you'll lose the grid state when they are invoked. I am still looking into a solution for this issue since there is no way to create custom commands inside a column. Sort of a huge pain.
0
luca bellon
Top achievements
Rank 1
answered on 15 May 2011, 12:51 PM
Hi John,
you must set EnableCustomBinding(true) in View too

@Html.Telerik().Grid(Model).Name("grid").EnableCustomBinding(true)

Luca
Tags
Grid
Asked by
John
Top achievements
Rank 1
Answers by
Merritt
Top achievements
Rank 1
luca bellon
Top achievements
Rank 1
Share this question
or