Reputation: 49
i have checked out the other questions/answers on here but none seemed to work with my senario.
Here is my viewmodel:
public class EmployeeListViewModel
{
public Employee Employee { get; set; }
public PaginatedList<Employee> Employees { get; set; }
public string Supervisor { get; set; }
}
Top of view:
@model MMSystem.ViewModels.EmployeeListViewModel
Controller:
public async Task<IActionResult> Index(string searchString, string currentFilter, int? pageNumber)
{
if (searchString != null)
{
pageNumber = 1;
}
else
{
searchString = currentFilter;
}
ViewData["CurrentFilter"] = searchString;
var employeeList = (from e in _context.Employees
join s in _context.Employees on e.Id equals s.SupervisorId
select new { Employee = s, Supervisor = e.FullName });
if (!String.IsNullOrEmpty(searchString))
{
employeeList = employeeList.Where(e => e.Employee.LastName.Contains(searchString)
||e.Employee.FirstName.Contains(searchString));
}
int pageSize = 3;
return View(new EmployeeListViewModel { Employees = await PaginatedList<Employee>.CreateAsync(employeeList.AsNoTracking(), pageNumber ?? 1, pageSize) });
}
And this is my error:
I have been trying everything to get it to work, but there seems to always be conversion or type conflict issues. (Note: everything worked fine prior to trying to add pagination)
Upvotes: 0
Views: 472
Reputation: 27825
Based on your code, we can find that your code snippet (as below) return a list of anonymous objects which contains Employee
and Supervisor
properties.
var employeeList = (from e in _context.Employees join s in _context.Employees on e.Id equals s.SupervisorId select new { Employee = s, Supervisor = e.FullName });
And at this code snippet await PaginatedList<Employee>.CreateAsync(employeeList.AsNoTracking(), pageNumber ?? 1, pageSize)
, it can not explicitly convert anonymous object list to System.Linq.IQueryable, which causes the issue.
cannot convert from '
System.Linq.IQueryable<<anonymous type: MMSystem.Models.Employee Employee, string Supervisor>>
' to 'System.Linq.IQueryable<MMSystem.Models.Employee>
'
To fix it, you can try modify the code like below.
return View(new EmployeeListViewModel { Employees = await PaginatedList<Employee>.CreateAsync(employeeList.Select(s=>s.Employee).AsNoTracking(), pageNumber ?? 1, pageSize) });
Besides, you can refer to this tutorial with example that shows how to implement paging etc functionality.
Upvotes: 1