如何在MVC CORE中通过ViewModel进行分页? [英] How do I paginate through a ViewModel in MVC CORE?

查看:70
本文介绍了如何在MVC CORE中通过ViewModel进行分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://docs.microsoft.com/zh-cn/aspnet/core/data/ef-mvc/sort-filter-page Contoso大学示例程序在分页示例页面上使用了模型

In the MVC CORE demo from https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/sort-filter-page the Contoso university sample program uses a model on the paginating example page

var students = from s in _context.Students
                           select s;
return View(await PaginatedList<Student>.CreateAsync(students.AsNoTracking(), page ?? 1, pageSize));

当视图模型像这样的完整实体传回时

while the viewmodel gets passed back as a complete entity like this

var viewModel = new InstructorIndexData();
viewModel.Instructors = await _context.Instructors
.Include(i => i.OfficeAssignment)
.Include(i => i.CourseAssignments)
.ThenInclude(i => i.Course)
.ThenInclude(i => i.Department)
.OrderBy(i => i.LastName)
.ToListAsync();

return View(viewModel);

如何对视图模型的返回记录进行分页.

我试图像这样将viewmodel传递到PaginatedList中.

I've tried passing the viewmodel into the PaginatedList like this.

return View(await PaginatedList<InstructorIndexData>.CreateAsync(viewModel.AsNoTracking(), page ?? 1, pageSize));

其中有错误

Error   CS1061  'InstructorIndexData' does not contain a definition for 'AsNoTracking' and no extension method 'AsNoTracking' accepting a first argument of type 'InstructorIndexData' could be found (are you missing a using directive or an assembly reference?) 

修改 ViewModel是

Edit The ViewModel is

namespace ContosoUniversity.Models.SchoolViewModels
{
    public class InstructorIndexData
    {
        public IEnumerable<Instructor> Instructors { get; set; }
        public IEnumerable<Course> Courses { get; set; }
        public IEnumerable<Enrollment> Enrollments { get; set; }
    }
}

将IEnumerable更改为IQueryable会导致以下情况

changing the IEnumerable to IQueryable causes the following

    var viewModel = new InstructorIndexData();
    viewModel.Instructors = await _context.Instructors
          .Include(i => i.OfficeAssignment)
          .Include(i => i.CourseAssignments)
            .ThenInclude(i => i.Course)
                .ThenInclude(i => i.Department)
          .OrderBy(i => i.LastName)
          .ToListAsync();

产生错误

Cannot implicitly convert type 'System.Collections.Generic.List<ContosoUniversity.Models.Instructor>' to 'System.Linq.IQueryable<ContosoUniversity.Models.Instructor>'. An explicit conversion exists (are you missing a cast?)

推荐答案

AsNoTracking()

是DbExtension程序集中定义的扩展方法. 属性"Instructors"必须具有实现"IQueryable"的类型才能启用此扩展方法.

is an Extension Method defnied in the DbExtension assembly. The property "Instructors" must be of a type that implements "IQueryable" to enable this extension method.

此外,在您提供的代码示例中

Also, in your provided code sample

return View(await PaginatedList<InstructorIndexData>.CreateAsync(viewModel.AsNoTracking(), page ?? 1, pageSize));

您正在对viewModel本身而不是属性"Instructors"使用扩展方法

You are using the extension method over the viewModel itself and not the property "Instructors"

这篇关于如何在MVC CORE中通过ViewModel进行分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆