添加到正常工作的

添加到正常工作的

本文介绍了如何将Paging添加到正常工作的“上下文”中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将分页添加到正常工作的上下文中。我从NuGet下载了PagedList。



错误代码:

I am trying to add paging to a working "Context". I have downloaded "PagedList" from NuGet.

Error Code:

An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

Additional information: The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'.




using PagedList;
using System.Data.Entity;

namespace AllIndexTables.Controllers
{
    public class HomeController : Controller
    {
        private testContext db = new testContext();
        public ActionResult Index(int? page)
        {
            int pageSize = 5;
            int pageNumber = (page ?? 1);
            return View(db.Data1.ToPagedList(pageNumber, pageSize));
        }
     }
}



Index.cshtml


Index.cshtml

@model IEnumerable<AllIndexTables.Models.Table1>
@using PagedList.Mvc;
@using PagedList;

<h2>Index</h2>

<center>
    <table>
        @foreach (var item in Model)
        {
            <tr>                {
                <td>
                    @item.Idt
                </td>
                <td>
                     @item.datetime0
                </td>
                <td>
                    @item.col1
                </td>
                <td>
                    @item.col2
                </td>
                <td>
                    @item.col3
                </td>
            </tr>
        }
    </table>
</center>

推荐答案

private testContext db = new testContext();
public ActionResult Index(int? page)
{
    var table1 = from s in db.Data1 select s;
    table1 = table1.OrderBy(s => s.Idt);
    int pageSize = 5;
    int pageNumber = (page ?? 1);
    return View(table1.ToPagedList(pageNumber, pageSize));
}


这篇关于如何将Paging添加到正常工作的“上下文”中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 05:35