I'm creating a Frequently Asked Questions web application. I followed Microsoft's tutorial for adding sorting, filtering, and paging to an ASP.NET MVC EF Core application, using the PaginatedList:
https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/sort-filter-page?view=aspnetcore-5.0#add-paging-to-students-index
My question was answered here:
https://forums.asp.net/t/2112532.aspx?How+to+Paging+the+Index+Page+implemented+by+ViewModel+
but I don't think I'm implementing the solution correctly. I understand that I need to create a collection of objects that need to be paginated, but when I try to change
var viewModel = new QuestionIndexData();
to
var viewModel = new List<QuestionIndexData>();
it then gives me an error that says 'List<QuestionIndexData> does not contain a definition for 'Questions'...
102147-homecontroller.txt102105-questionindexdata.txt
If I add
public List<QuestionAndAnswer> Questions { get; set; }
to the QuestionIndexData model, the error does not go away.
Code truncated for brevity. Full code attached. Commented lines are solutions I have tried unsuccessfully.
QuestionIndexData model created following this tutorial:
https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/read-related-data?view=aspnetcore-5.0#create-an-instructors-page
public class QuestionIndexData
{
public IEnumerable<QuestionAndAnswer> Questions { get; set; }
public IEnumerable<Tag> Tags { get; set; }
public IEnumerable<QuestionTag> QuestionTags { get; set; }
public IEnumerable<Vote> Votes { get; set; }
//public IList<QuestionAndAnswer> Questions { get; set; }
//public PaginatedList<QuestionAndAnswer> PagedQuestions { get; set; }
}
Home Controller Index method:
public async Task<IActionResult> Index(
int? id,
string searchString,
int? pageNumber)
{
//...
var viewModel = new QuestionIndexData();
if (!String.IsNullOrEmpty(searchString))
{
//here is the error in my screenshot
viewModel.Questions = await _context.QuestionAndAnswers
.Include(i => i.Votes)
.Include(i => i.QuestionTags)
.ThenInclude(i => i.Tags)
.Where(s =>
s.Question.Contains(searchString) ||
s.Answer.Contains(searchString))
.AsNoTracking()
.OrderByDescending(i => i.Votes.Count)
.ToListAsync();
}
else
{
viewModel.Questions = await _context.QuestionAndAnswers
.Include(i => i.Votes)
.Include(i => i.QuestionTags)
.ThenInclude(i => i.Tags)
.AsNoTracking()
.OrderByDescending(i => i.Votes.Count)
.ToListAsync();
}
//return View(await PaginatedList<Student>.CreateAsync(students.AsNoTracking(), pageNumber ?? 1, pageSize));
//return View(await PaginatedList<QuestionAndAnswer>.CreateAsync(viewModel.Questions, pageNumber ?? 1, pageSize));
//return View(await PaginatedList<QuestionAndAnswer>.CreateAsync(viewModel.Questions.AsQueryable(), pageNumber ?? 1, pageSize));
//returns error: InvalidOperationException: The provider for the source 'IQueryable' doesn't implement 'IAsyncQueryProvider'. Only providers that implement 'IAsyncQueryProvider' can be used for Entity Framework asynchronous operations.
//return View(await PaginatedList<QuestionIndexData>.CreateAsync(viewModel.Questions.AsQueryable().AsNoTracking(), pageNumber ?? 1, pageSize));
//return View(new QuestionIndexData { Questions = await PaginatedList<QuestionAndAnswer>.CreateAsync(viewModel.Questions.AsQueryable().AsNoTracking(), pageNumber ?? 1, pageSize) });
//int pageSize = 3;
//this works without pagination
return View(viewModel);
}


