简体   繁体   中英

Telerik MVC Grid and Paging/Sorting/Filtering in database

I am using Telerik Grid MVC with AJAX binding.

I thought that if I provide IQueryable to the grid, paging/sorting/filtering would be done at the database server. Like so:

[GridAction]
public ActionResult Select()
{
    return View(new GridModel(Mapper.Map<IEnumerable<DokumentVM>>(db.Dokumenti)));        
}

I created test data of about 10000 documents in database, and upper command resulted in each of them hauled into the grid. And obviously, it takes forever.

Grid is bound via AJAX, like this:

@(Html.Telerik().Grid<ViewModels.DokumentVM>()
.DataBinding(b => b.Ajax().Select("Select", "Dokument"))
.Pageable(p => p.PageSize(20))
.Sortable(s => s.SortMode(GridSortMode.MultipleColumn).OrderBy(m => { m.Add("Date").Descending(); m.Add("Number").Descending(); })))

Inside Select ActionMethod, when inspecting Request (its Form), I see that grid sends all info needed for proper functioning:

page:     1
size:     20
orderBy:  Date-desc~Number-desc

But when I further inspect SQL command sent to database, I see that there is only SELECT command present, no WHERE, no ORDER by, no nothing, which brings down all my data.

I am wondering if it is possible for Paging/Sorting/Filtering to work automatically, or do I need to translate info sent by the grid into SQL commands myself. I was under impression that all I needed is to provide IQueryable and Grid would do the rest. But that's not working for me.

Maybe I am doing something wrong, or maybe this is not even possible?

The problem is using AutoMapper! when you call this line :

 return View(new GridModel(Mapper.Map<IEnumerable<DokumentVM>>(db.Dokumenti)));

at first , AutoMapper tries to convert all of your db.Dokumenti rows into new form in memory function. After that the mapped data passes to GridModel. All things about paging and ordering happens inside this class.

So you should avoid using AutoMapper at this level and using a different strategy. for example you can make a query like this :

var q= from r in db.Dokumenti select new DokumentVM(){ ... }

and then pass the q variable into GridModel class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM