如何在asp.net core中使用分页列表实现多分页?
我是asp.net核心的新手。我有一个按年份分组的列表,我想把它们分为子项。我现在可以使用pagedList翻页一个简单的列表。非常感谢 这是我的剃须刀页面:如何在asp.net core中使用分页列表实现多分页?,asp.net,model-view-controller,pagedlist,Asp.net,Model View Controller,Pagedlist,我是asp.net核心的新手。我有一个按年份分组的列表,我想把它们分为子项。我现在可以使用pagedList翻页一个简单的列表。非常感谢 这是我的剃须刀页面: <ul class="list-group"> @foreach (var item in Model) { <li class="d-flex border-0 bg-transparent"> @item.Key </li>
<ul class="list-group">
@foreach (var item in Model)
{
<li class="d-flex border-0 bg-transparent">
@item.Key
</li>
@foreach (var p in item.GroupBy(x => x.KeyA).OrderBy(o => o.Key))
{
<li class="d-flex border-0 bg-transparent my-2">
@p.Key
</li>
@foreach (var a in item.Where(x => x.Data.Year == item.Key && x.KeyA == p.Key).OrderBy(o => o.Customer.Name).Select(a => a.Customer).Distinct())
{
//I would like to paginate thesse items
<li class="list-group-item d-flex justify-content-between align-items-center border my-1 p-0 pl-2 bg-white">
@a.Nome
<a asp-action="Details" asp-controller="Animais" asp-route-id="@a.CustomerId" class="btn btn-success btn-sm d-none d-md-block">SEE MORE</a>
<a asp-action="Details" asp-controller="Animais" asp-route-id="@a.CustomerId" class="btn btn-success btn-sm d-md-none">
<span class="fas fa-eye"></span>
</a>
</li>
}
}
}
</ul>
@foreach(模型中的var项目)
{
-
@项目.关键
@foreach(item.GroupBy(x=>x.KeyA.OrderBy(o=>o.Key))中的var p)
{
-
@p、 钥匙
@foreach(item.Where(x=>x.Data.Year==item.Key&&x.KeyA==p.Key)中的变量a.OrderBy(o=>o.Customer.Name)。选择(a=>a.Customer.Distinct())
{
//我想为这些项目编页码
-
@a、 诺姆
查看更多
}
}
}
下面是使用引导分页的解决方案
在ProductController.cs中:
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace TestControllers
{
public class ProductController : Controller
{
private readonly ILogger<ProductController> _logger;
public ProductController(ILogger<ProductController> logger)
{
_logger = logger;
}
public IActionResult Products()
{
//Generate test products data
var products = Enumerable.Range(1,1000).Select(i=>
new Product(){Id=i,BrandId=i%10+1, CategoryId=i%20 + 1, ExpirationDate="12/"+(i%31 + 1) + "/2020", Name="Product"+i, OriginCity="City"+(i%40 + 1), OriginCountry="Country"+(i%50 + 1), ProducerName="Producer"+ i+100 + 1, ProductionDate="1/"+(i%31 + 1) + "/2020"}
).ToList();
return View(products);
}
}
public class Product
{
public int Id { get; set; }
public int CategoryId { get; set; }
public int BrandId { get; set; }
public string Name { get; set; }
public string ProducerName { get; set; }
public string OriginCity { get; set; }
public string OriginCountry { get; set; }
public string ProductionDate { get; set; }
public string ExpirationDate { get; set; }
}
}
使用System.Linq;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Extensions.Logging;
命名空间测试控制器
{
公共类ProductController:控制器
{
专用只读ILogger\u记录器;
公共产品控制器(ILogger记录器)
{
_记录器=记录器;
}
公共产品(
{
//生成测试产品数据
var products=Enumerable.Range(11000)。选择(i=>
新产品(){Id=i,BrandId=i%10+1,CategoryId=i%20+1,ExpirationDate=“12/”+(i%31+1)+“/2020”,Name=“Product”+i,OriginCity=“City”+(i%40+1),OriginCountry=“Country”+(i%50+1),ProducerName=“Producer”+i+100+1,ProductionDate=“1/”+(i%31+1)+“/2020}
).ToList();
返回视图(产品);
}
}
公共类产品
{
公共int Id{get;set;}
public int CategoryId{get;set;}
public int BrandId{get;set;}
公共字符串名称{get;set;}
公共字符串ProducerName{get;set;}
公共字符串原始性{get;set;}
公共字符串OriginCountry{get;set;}
公共字符串ProductionDate{get;set;}
公共字符串过期日期{get;set;}
}
}
在Product.cshtml中:
@model List<TestControllers.Product>
@using System.Linq;
@{
ViewData["Title"] = "Products Page";
var category = this.Context.Request.Query["category"];
var page = this.Context.Request.Query["page"];
var categoryInt = !string.IsNullOrEmpty(category) ? int.Parse(category) : 1;
var pageInt = !string.IsNullOrEmpty(page) ? int.Parse(page) : 1;
var productsPerPage = 10;
//Get pager contents
Func<string,string, int, int,int, Func<int,Dictionary<string,string>>, string> getPager = (controller,action,pages, currentPage,numberOfPages, getParameters) =>
{
//Get query string for a given page number
Func<int, string> getQueryString = (page1) =>
{
var queryString = getParameters(page1).ToList().Aggregate("", (content, next) =>
{
content = !string.IsNullOrEmpty(content) ? content + "&" : content;
content += string.Format("{0}={1}", next.Key, next.Value);
return content;
});
queryString = !string.IsNullOrEmpty(queryString) ? "?" + queryString : queryString;
return queryString;
};
var pager = "<nav aria-label=\"...\"><ul class=\"pagination .pagination-lg\">";
if(currentPage>1)
{
pager+= "<li class=\"page-item\"><a class=\"page-link\" href=\"" + string.Concat("/", controller, "/", action, getQueryString(currentPage-1)) + "\">Previous</a></li>";
}
else
{
pager += "<li class=\"page-item disabled\"><span class=\"page-link\">Previous</span></li>";
}
var renderedPageSets = new List<List<int>>();
var renderedPages = 0;
if(pages<= numberOfPages)
{
renderedPageSets.Add(Enumerable.Range(1, pages).ToList());
}
else
{
while (renderedPages < pages)
{
var lastSetPage = renderedPages + numberOfPages;
if (lastSetPage <= pages)
{
renderedPageSets.Add(Enumerable.Range(++renderedPages, numberOfPages).ToList());
}
else
{
renderedPageSets.Add(Enumerable.Range(pages - numberOfPages + 1, numberOfPages).ToList());
}
if(renderedPageSets.Last().Contains(currentPage))
{
break;
}
renderedPages = lastSetPage;
}
}
var renderedPageSet = renderedPageSets.First(s => s.Contains(currentPage));
if(renderedPageSet.First()!=1)
{
pager += "<li class=\"page-item disabled\"><span class=\"page-link\">...</span></li>";
}
pager = renderedPageSet.Aggregate(pager, (currentPagerContent, nextPage) => {
currentPagerContent += "<li class=\"page-item"+(currentPage==nextPage? " active":"") + " \"><a class=\"page-link\" href=\""+string.Concat("/",controller,"/",action, getQueryString(nextPage)) + "\">"+nextPage+"</a></li>";
return currentPagerContent;
});
if (renderedPageSet.Last() != pages)
{
pager += "<li class=\"page-item disabled\"><span class=\"page-link\">...</span></li>";
}
if (currentPage < pages)
{
pager += "<li class=\"page-item\"><a class=\"page-link\" href=\"" + string.Concat("/", controller, "/", action, getQueryString(currentPage + 1)) + "\">Next</a></li>";
}
else
{
pager += "<li class=\"page-item disabled\"><span class=\"page-link\">Next</span></li>";
}
pager += "</li></ul></nav>";
return pager;
};
}
<div class="text-center">
<h1 class="display-4">Products</h1>
</div>
<div class="card" style="">
<div class="card-header">
<div>
@{
var categories = Model.Select(i => i.CategoryId).Distinct().Count();
}
<h2>Categories:</h2> @Html.Raw(getPager("Product", "Products", categories, categoryInt, 10, (category1) => { return new Dictionary<string, string>() { { "page", "1" }, { "category", category1.ToString() } }; }))
</div>
<h3>Products in category @categoryInt</h3>
</div>
<br />
<a asp-action="Add" asp-controller="Product" class="btn btn-success btn-sm" style="width:148px">Add a New Product</a>
<table class="table table-bordered">
<tr>
<th>Category</th>
<th>Name</th>
<th>Producer Name</th>
<th>Origin City</th>
<th>Origin Country</th>
<th>Production Date</th>
<th>Expiration Date</th>
<th> </th>
</tr>
@foreach (var product in Model.Where(i => i.CategoryId == categoryInt).Skip((pageInt - 1) * productsPerPage).Take(productsPerPage))
{
<tr>
<td>@product.CategoryId</td>
<td>@product.Name</td>
<td>@product.ProducerName</td>
<td>@product.OriginCity</td>
<td>@product.OriginCountry</td>
<td>@product.ProductionDate</td>
<td>@product.ExpirationDate</td>
<td>
<a asp-action="Edit" asp-controller="Product" asp-route-id="@product.Id" class="btn btn-warning btn-sm">Edit</a>
<a asp-action="Delete" asp-controller="Product" asp-route-id="@product.Id" class="btn btn-danger btn-sm">Delete</a>
</td>
</tr>
}
</table>
<div>
@{
var categoryProductsPages = (int)Math.Ceiling(((double)Model.Where(i => i.CategoryId == categoryInt).Count()) / productsPerPage);
}
@Html.Raw(getPager("Product", "Products", categoryProductsPages, pageInt, 10, (page1) => { return new Dictionary<string, string>() { { "page", page1.ToString() }, { "category", category } }; }))
</div>
</div>
@型号列表
@使用System.Linq;
@{
ViewData[“Title”]=“产品页面”;
var category=this.Context.Request.Query[“category”];
var page=this.Context.Request.Query[“page”];
var categoryInt=!string.IsNullOrEmpty(category)?int.Parse(category):1;
var pageInt=!string.IsNullOrEmpty(第页)?int.Parse(第页):1;
var productsPerPage=10;
//获取寻呼机内容
Func getPager=(控制器、操作、页面、当前页面、页面数、getParameters)=>
{
//获取给定页码的查询字符串
Func getQueryString=(第1页)=>
{
var queryString=getParameters(第1页).ToList().Aggregate(“,(内容,下一页)=>
{
content=!string.IsNullOrEmpty(content)?content+“&”:content;
content+=string.Format(“{0}={1}”,next.Key,next.Value);
返回内容;
});
queryString=!string.IsNullOrEmpty(queryString)?“?”+queryString:queryString;
返回查询字符串;
};
var pager=“;
如果(当前页面>1)
{
寻呼机+=“”;
}
其他的
{
寻呼机+=“- ”>Previous
”;
}
var renderedPageSets=新列表();
var renderedPages=0;
如果(第页){
currentPagerContent+=“”;
返回当前页面内容;
});
if(renderedPageSet.Last()!=页面)
{
寻呼机+=“- …
”;
}
如果(当前页<页)
{
寻呼机+=“”;
}
其他的
{
寻呼机+=“- ”>Next
”;
}
寻呼机+=“
”;
传呼机;
};
}
产品
@{
var categories=Model.Select(i=>i.CategoryId.Distinct().Count();
}
Categories:@Html.Raw(getPager(“产品”,“产品”,类别,categoryInt,10,(category1)=>{returnnewdictionary(){{“页面”,“1”},{“类别”,category1.ToString()};}))
@categoryInt类别中的产品
很难说出您希望看到的视图的结构,但您可以使用它作为替代方案。为了开始使用ASP.NET核心MVC,请更好地考虑,您有一个产品列表,按类别分组,并按品牌分组。我只想在类别内对这些产品进行分页。我正在其他分页中使用X.PagedList.PagedList。有关引导分页解决方案,请参阅我的文章。寻呼机代码可以使用多种方法进行调整。谢谢EricN!我会根据你的例子来尝试工作。很高兴我能帮上忙。你能把我的帖子作为答案还是投票给我?