简体   繁体   中英

ASP.net MVC4 Grid in EF data Model

I'm trying to use GRID.MVC in my project, but i got this error System.NotSupportedException: The Skip method is only supported for sorted input in LINQ to Entities. The OrderBy method must be called before the Skip method. System.NotSupportedException: The Skip method is only supported for sorted input in LINQ to Entities. The OrderBy method must be called before the Skip method. at line 27 at _grid.cshtml file :

 Ligne 25 : @helper RenderGridBody()
Ligne 26 : {
Ligne 27 :     if (!Model.ItemsToDisplay.Any())
Ligne 28 :     {
Ligne 29 :     <tr class="grid-empty-text">

and this's my view :

@Html.Grid(Model).Columns(columns =>
                    {
                        columns.Add(item => item.OFFRE_ID).Titled("Custom column title").SetWidth(110);
                        columns.Add(item => item.REGION.NOM).Sortable(true);
                        columns.Add(item => item.DESCRIPTION).Sortable(false);
                        columns.Add(item => item.OFFRE_DATE).Sortable(true);
                    }).WithPaging(20)

Please how to fix it ?

Just so you know, .WithPaging causes this. It makes the GridView accept only sorted list. That means that passing an unsorted list will throw an exception.

// Passing this to the gridview will throw an exception because it is not sorted.
var offre = db.OFFRE.Include(o => o.REGION); 

The solution is to "order" your list before passing it into the grid view.

var offre = db.OFFRE
    .Include(o => o.REGION)
    .OrderBy(c => c.OFFRE_ID); // This converts the list into a sorted list.

return View(offre);

To Slove this Problem I had to Add OrderBy in the Controler result like this :

public ActionResult Index()
        {
            var offre = db.OFFRE.Include(o => o.REGION);
            return View(offre.OrderBy(c => c.OFFRE_ID));
        }

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