需要ASP.NET Core 2.0分页帮助

时间:2018-02-13 05:12:45

标签: c# asp.net-core-mvc entity-framework-core

再次需要帮助。 所以我目前正在研究ASP.NET Core 2.0 Paging,但它无法正常工作。

我在这里按照教程https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/sort-filter-page

但是,教程是使用Entity Framework dateset还是IQuery?然而,我正在使用List<&gt ;.有谁知道如何解决这个问题或将我的List转换为EF DataSet或什么?

Controller Paginated.cs:

public static async Task<PaginatedList<T>> CreateAsync(IQueryable<T> source, int pageIndex, int pageSize)
{
    var count = await source.CountAsync();
    var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
    return new PaginatedList<T>(items, count, pageIndex, pageSize);
}

控制器:

public async Task<IActionResult> Index(string sortOrder, string currentFilter, string searchString, int? page)
{
    ViewData["CurrentSort"] = sortOrder;
    ViewBag.CurrentSort = sortOrder;
    var students = addList();

    ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
    if (searchString != null)
    {
        page = 1;
    }
    else
    {
        searchString = currentFilter;
    }

    ViewBag.CurrentFilter = searchString;


    switch (sortOrder)
    {
        case "name_desc":
            students = students.OrderByDescending(s => s.Name).ToList();
            break;
        default:
            break;
    }

    if (searchString != null)
    {
        page = 1;
    }
    else
    {
        searchString = currentFilter;
    }

    ViewData["CurrentFilter"] = searchString;
    int pageSize = 3;
    return View(await PaginatedList<Student>.CreateAsync(students.AsNoTracking(), page ?? 1, pageSize));
    // return View(PaginatedList<Student>(students, page ?? 1, pageSize));
}

如果您查看上面的代码,它只有在从EF检索数据集时才有效。

CountAsync(),students.AsNoTracking()因为是List而无法正常工作。

任何人都知道我该怎么办? 如果您需要什么,请告诉我。

谢谢!

编辑:

public IQueryable<Student> addList()
{
    var studentList = new List<Student>();
    studentList.Add(new Student
    {
        Id = 1,
        Name = "David",
        ClassName = "Moria"
    });

    studentList.Add(new Student
    {
        Id = 2,
        Name = "Jenny",
        ClassName = "Moria"
    });

    studentList.Add(new Student
    {
        Id = 3,
        Name = "Crystal",
        ClassName = "Moria"
    });

    studentList.Add(new Student
    {
        Id = 4,
        Name = "jenny",
        ClassName = "Moria"
    });

    studentList.Add(new Student
    {
        Id = 5,
        Name = "Andria",
        ClassName = "Moria"
    });

    studentList.Add(new Student
    {
        Id = 6,
        Name = "Sweet",
        ClassName = "Moria"
    });

    studentList.Add(new Student
    {
        Id = 7,
        Name = "Susan",
        ClassName = "Moria"
    });
    return studentList.AsQueryable();
}

1 个答案:

答案 0 :(得分:4)

您的addList()只是创建一组内存中的硬编码数据。使用异步方法(实际上是降低性能)或使用IQueryable(您不查询外部数据源)毫无意义。

我建议将addlist方法重命名为更有意义的方法,比如FetchStudents()。由于您在方法中生成List<Student>,因此应返回IList<Student>

public IList<Student> FetchStudents()
{
    ....
    return studentList;
}

接下来,将CreateAsync()方法更改为接受IEnumerable<T>并删除异步调用

public static PaginatedList<T> Create(IEnumerable<T> source, int pageIndex, int pageSize)
{
    var count = source.Count();
    var items = source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
    return new PaginatedList<T>(items, count, pageIndex, pageSize);
}

最后将Index()方法修改为

public IActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
    ....
    var students = FetchStudents();
    ....
    return View(PaginatedList<Student>.Create(students, page ?? 1, pageSize));
}