【问题标题】:I have problem using ReflectionIT.Mvc.Paging for pagination in my Asp.NET Core 2.2 project我在我的 Asp.NET Core 2.2 项目中使用 ReflectionIT.Mvc.Paging 进行分页时遇到问题
【发布时间】:2020-01-01 17:21:43
【问题描述】:

第一页的分页工作正常,它也可以获取结果。

但问题是,每当我点击分页中的任何页码(假设我点击了 2)时,它都会搜索 Index.cshtml 文件,而不是转到该特定控制器的方法。

下面的代码在HomeController'. But it will search forIndexmethod ofHomeController`内部的方法GetCustomers的视图中`

 <nav>
    @await this.Component.InvokeAsync(typeof(ReflectionIT.Mvc.Paging.PagerViewComponent), new { pagingList = this.Model }))
 </nav>

我应该对代码进行哪些更改以使其转到方法GetCustomers?

【问题讨论】:

标签: c# asp.net-core asp.net-core-2.2


【解决方案1】:

您可以将 Action 属性设置为您的 PagingList 对象,如下所示:

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);            
}

【讨论】:

    【解决方案2】:

    我认为您在视图中遗漏了一些代码,

    尝试更改它来自

    <nav>
    @await this.Component.InvokeAsync(typeof(ReflectionIT.Mvc.Paging.PagerViewComponent), 
    new { pagingList = this.Model }))
    </nav>
    

    到

    <nav aria-label="Suppliers navigation example">
    @await this.Component.InvokeAsync("Pager", new { pagingList = this.Model })
    </nav>
    

    并将其添加到您的视图顶部

    @model ReflectionIT.Mvc.Paging.PagingList<"Your Model">
    @using ReflectionIT.Mvc.Paging
    @addTagHelper *, ReflectionIT.Mvc.Paging
    

    并将其添加到您的控制器中

    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);
    }
    

    更多详情请参考此网址

    https://www.reflectionit.nl/blog/2017/paging-in-asp-net-core-mvc-and-entityframework-core

    【讨论】:

      【解决方案3】:

      公共异步任务索引(int page = 1)

      而不是页面使用 pageIndex 。会是这样的

      公共异步任务索引(int pageIndex = 1)

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。
      猜你喜欢
      • 2021-09-10
      • 2021-10-09
      • 1970-01-01
      • 2021-02-15
      • 2022-01-01
      • 2018-03-03
      • 2022-06-11
      • 2020-05-27
      • 2021-08-09
      相关资源
      最近更新 更多