Reputation: 301
The pagination is working fine for first page and it fetches results also.
But the problem is whenever I click on any page number in pagination (suppose I clicked 2), then it will search for Index.cshtml file instead of going to the method of that particular controller.
The below code is in the View of method GetCustomers
which is inside HomeController'. But it will search for
Indexmethod of
HomeController`
<nav>
@await this.Component.InvokeAsync(typeof(ReflectionIT.Mvc.Paging.PagerViewComponent), new { pagingList = this.Model }))
</nav>
What changes should I make in the code so that it will go to method GetCustomers
?
Upvotes: 2
Views: 1106
Reputation: 1
public async Task Index(int page = 1)
instead of page use pageIndex . it will be like that
public async Task Index(int pageIndex = 1)
Upvotes: 0
Reputation: 753
I think you have missed some code in your view ,
try changing it From
<nav>
@await this.Component.InvokeAsync(typeof(ReflectionIT.Mvc.Paging.PagerViewComponent),
new { pagingList = this.Model }))
</nav>
To
<nav aria-label="Suppliers navigation example">
@await this.Component.InvokeAsync("Pager", new { pagingList = this.Model })
</nav>
and add this at top of the your view
@model ReflectionIT.Mvc.Paging.PagingList<"Your Model">
@using ReflectionIT.Mvc.Paging
@addTagHelper *, ReflectionIT.Mvc.Paging
and add this in your controller
public async Task<IActionResult> Index(int page = 1) {
var qry = _context.Suppliers.AsNoTracking().OrderBy(p => p.CompanyName);
var model = await PagingList.CreateAsync(qry, 10, page);
return View(model);
}
and for more details Refer this URL
https://www.reflectionit.nl/blog/2017/paging-in-asp-net-core-mvc-and-entityframework-core
Upvotes: 0
Reputation: 36655
You could set Action property to your PagingList object like below:
public async Task<IActionResult> Index(int page = 1)
{
var qry = _context.Customer.AsNoTracking().OrderBy(c => c.Name);
var model = await PagingList.CreateAsync(qry, 10, page);
model.Action = "GetCustomers";
return View(model);
}
Upvotes: 1