“我的索引”剃刀页面包含一个项目列表,可以使用下拉菜单中的多个条件对其进行过滤。我想添加一个分页,以减少记录数大于10(当前设置为3以进行测试)时在页面上显示的记录数。我已经搜索并阅读了许多解决方案,但是还没有找到将它们集成到当前代码中的方法。
我已经根据标准的Microsoft教程https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/sort-filter-page?view=aspnetcore-3.0
创建了PaginatedList.cs。我的Index.cshtml.cs页面包含以下代码:
public PaginatedList<Project> Projects { get; set; }
public IList<Project> Project { get; set; }
public IList<Resource> Resources { get; set; }
public SelectList FOOptions { get; set; }
public string CurrentFOFilter { get; set; }
public SelectList Options { get; set; }
public string CurrentDSMFilter { get; set; }
public async Task<IActionResult> OnGetAsync(List<int> fosearchString, List<int> dsmsearchString, int? pageIndex)
{
FOOptions = new SelectList(_context.Resource, nameof(Resource.Id), nameof(Resource.LongName));
List<int> CurrentFOFilter = fosearchString;
Options = new SelectList(_context.Resource, nameof(Resource.Id), nameof(Resource.LongName));
List<int> CurrentDSMFilter = dsmsearchString;
if (fosearchString.Count == 0 && dsmsearchString.Count == 0)
{
pageIndex = 1;
int pageSize = 3;
Resources = await _context.Resource.ToListAsync();
Project = await _context.Project.Include(p => p.Resource).ToListAsync();
}
else if (fosearchString.Count != 0 && dsmsearchString.Count == 0)
{
pageIndex = 1;
int pageSize = 3;
Resources = await _context.Resource.ToListAsync();
Project = await _context.Project
.Include(p => p.Resource)
.Where(s => fosearchString.Contains(s.FOId))
.ToListAsync();
}
else if (fosearchString.Count == 0 && dsmsearchString.Count != 0)
{
pageIndex = 1;
int pageSize = 3;
Resources = await _context.Resource.ToListAsync();
Project = await _context.Project
.Include(p => p.Resource)
.Where(s => dsmsearchString.Contains(s.DSMId))
.ToListAsync();
}
else
{
pageIndex = 1;
int pageSize = 3;
Resources = await _context.Resource.ToListAsync();
Project = await _context.Project
.Include(p => p.Resource)
.Where(s => fosearchString.Contains(s.FOId) && dsmsearchString.Contains(s.DSMId))
.ToListAsync();
}
return Page();
}
然后,我还将在我的Index.cshtml页面中添加类似于以下内容的内容:
@{
var prevDisabled = !Model.Projects.HasPreviousPage ? "disabled" : "";
var nextDisabled = !Model.Projects.HasNextPage ? "disabled" : "";
}
<a asp-page="./Index"
asp-route-pageIndex="@(Model.Projects.PageIndex - 1)"
asp-route-currentFilter="@Model.CurrentFOFilter && @Model.CurrentDSMFilter"
class="btn btn-primary @prevDisabled">
Previous
</a>
<a asp-page="./Index"
asp-route-pageIndex="@(Model.Projects.PageIndex + 1)"
asp-route-currentFilter="@Model.CurrentFOFilter && @Model.CurrentDSMFilter"
class="btn btn-primary @nextDisabled">
Next
</a>
我已经有了过滤器的表单代码,如下所示:
<form asp-page="./Index" method="get">
<div class="dropdown col-4 no-gutters">
<div class="input-group mb-3">
<select class="custom-select" name="fosearchString" value="@Model.CurrentFOFilter" asp-items="Model.FOOptions" selected="selected"><option value="">Filter by FO...</option></select><text> </text>
<select class="custom-select" name="dsmsearchString" value="@Model.CurrentDSMFilter" asp-items="Model.Options" selected="selected"><option value="">Filter by DSM...</option></select><text> </text>
</div>
<input type="submit" value="Filter" class="btn btn-primary" /><text> </text><input type="submit" action="/Projects/Index" value="Back to full List" class="btn btn-primary" />
</div>
</form>
如何翻译Microsoft解释的代码以将其包含在我的代码中:
Project = await _context.Project
.Include(p => p.Resource)
.Where(s => fosearchString.Contains(s.FOId) && dsmsearchString.Contains(s.DSMId))
.ToListAsync();
如果我按如下所示修改查询,Visual Studio会标记许多错误:
Projects = await PaginatedList<Project>.CreateAsync(
Project.AsNoTracking(), pageIndex ?? 1, pageSize);
我是从错误的角度出发吗?我想要做的就是添加一个分页,以使我的当前代码与各种搜索过滤器保持一致。
预先感谢您的帮助。
答案 0 :(得分:1)
感谢Laz Ziya。该答案基于链接http://www.ziyad.info/en/articles/38-How_to_build_an_efficient_pagination_system
下提供的信息您需要使用程序包管理器控制台安装LazZiya.TagHelpers nuget程序包,并将LazZiya.TagHelpers添加到_ViewImports.cshtml页面中,以作为显示分页的先决条件。
这是我的Index.cshtml.cs PageModel中的代码
public class IndexModel : PageModel
{
private readonly ManyToManyRelationship.Data.ManyToManyRelationshipContext _context;
//page number variable
[BindProperty(SupportsGet = true)]
public int P { get; set; } = 1;
//page size variable
[BindProperty(SupportsGet = true)]
public int S { get; set; } = 10;
public IndexModel(ManyToManyRelationship.Data.ManyToManyRelationshipContext context)
{
_context = context;
}
public IList<Project> Project { get; set; }
public int TotalRecords { get; set; } = 0;
public IList<Resource> Resources { get; set; }
public SelectList FOOptions { get; set; }
public string CurrentFOFilter { get; set; }
public SelectList Options { get; set; }
public string CurrentDSMFilter { get; set; }
public async Task<IActionResult> OnGetAsync(List<int> fosearchString, List<int> dsmsearchString)
{
FOOptions = new SelectList(_context.Resource, nameof(Resource.Id), nameof(Resource.LongName));
List<int> CurrentFOFilter = fosearchString;
Options = new SelectList(_context.Resource, nameof(Resource.Id), nameof(Resource.LongName));
List<int> CurrentDSMFilter = dsmsearchString;
if (fosearchString.Count == 0 && dsmsearchString.Count == 0)
{
Project = await _context.Project.Include(p => p.Resource).ToListAsync();
TotalRecords = Project.Count();
Resources = await _context.Resource.ToListAsync();
Project = await _context.Project.Include(p => p.Resource).Skip((P - 1) * S).Take(S).ToListAsync();
}
else if (fosearchString.Count != 0 && dsmsearchString.Count == 0)
{
Project = await _context.Project.Include(p => p.Resource).Where(s => fosearchString.Contains(s.FOId)).ToListAsync();
TotalRecords = Project.Count();
Resources = await _context.Resource.ToListAsync();
Project = await _context.Project
.Include(p => p.Resource)
.Where(s => fosearchString.Contains(s.FOId)).Skip((P - 1) * S).Take(S)
.ToListAsync();
}
else if (fosearchString.Count == 0 && dsmsearchString.Count != 0)
{
Project = await _context.Project.Include(p => p.Resource).Where(s => dsmsearchString.Contains(s.DSMId)).ToListAsync();
TotalRecords = Project.Count();
Resources = await _context.Resource.ToListAsync();
Project = await _context.Project
.Include(p => p.Resource)
.Where(s => dsmsearchString.Contains(s.DSMId)).Skip((P - 1) * S).Take(S)
.ToListAsync();
}
else
{
Project = await _context.Project.Include(p => p.Resource).Where(s => fosearchString.Contains(s.FOId) && dsmsearchString.Contains(s.DSMId)).ToListAsync();
TotalRecords = Project.Count();
Resources = await _context.Resource.ToListAsync();
Project = await _context.Project
.Include(p => p.Resource)
.Where(s => fosearchString.Contains(s.FOId) && dsmsearchString.Contains(s.DSMId)).Skip((P - 1) * S).Take(S)
.ToListAsync();
}
return Page();
}
}
,您需要将分页代码添加到Index.cshtml页面:
<paging page-no="Model.P"
page-size="Model.S"
total-records="Model.TotalRecords"
query-string-value="@(Request.QueryString.Value)"
show-prev-next="true"
show-total-pages="true"
show-total-records="true"
show-page-size-nav="true"
show-first-numbered-page="true"
show-last-numbered-page="true">
</paging>