Reputation: 329
Needed help again. So I am currently working on ASP.NET Core 2.0 Paging and it's not working properly.
I followed the tutorial here https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/sort-filter-page
However, the thing is that the tutorial is using Entity Framework dateset or IQuery? I however is using List<>. Does anyone know how to solve this problem or convert my List into EF DataSet or something?
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);
}
Controller:
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));
}
If you look at the code above, it only works if it is dataset retrieve from EF.
The CountAsync(), students.AsNoTracking() is not working because it is a List.
Anyone know what should I do? Let me know if you need anything.
Thanks!
EDit:
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();
}
Upvotes: 4
Views: 4180
Reputation:
Your addList()
is just creating an in-memory set of hard coded data. There is no point using async methods (and it is actually degrading performance) or using IQueryable
(your not querying an external data source).
I suggest rename your addlist
method to something more meaningful, say FetchStudents()
. Since your generating List<Student>
in the method, it should return IList<Student>
public IList<Student> FetchStudents()
{
....
return studentList;
}
Next change the CreateAsync()
method to accept IEnumerable<T>
and remove the async calls
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);
}
and finally modify the Index()
method to
public IActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
....
var students = FetchStudents();
....
return View(PaginatedList<Student>.Create(students, page ?? 1, pageSize));
}
Upvotes: 4