我正在尝试将分页添加到我的 .net core mvc 6.0 项目中,因为我创建了一个分页类,这里是代码
public class PaginatedList<T>: List<T>
{
public int PageIndex { get; set; }
public int TotalPages { get; set; }
public PaginatedList(List<T> items, int count, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
TotalPages = (int)Math.Ceiling(count/(double)pageSize);
this.AddRange(items);
}
public bool HasPreviousPage => PageIndex > 1;
public bool HasNextPage => PageIndex < TotalPages;
public static PaginatedList<T> Create (List<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);
}
}
并在 show.cshtml 中显示分页,这里是代码
@model PaginatedList<PropertyListWithImageViewModel> // Specify the model type
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<link rel=" stylesheet" href="~/Home/css/show.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
.property-cell.spacer {
width: 40px; /* adjust this value to set the desired spacing */
}
</style>
<!-- start subheader -->
<section class="subHeader page">
<div class="container">
<h1></h1>
<form class="searchForm" method="post" action="#">
<input type="hidden" name="search" value="Search our site" />
</form>
</div><!-- end subheader container -->
</section><!-- end subheader section -->
<section id="price-drop" class="block">
<div class="container">
<header class="section-title">
<h2>Properties for You</h2>
</header>
@if (Model.Count == 0)
{
<p>No properties found.</p>
}
else
{
<table class="property-table mx-auto">
@for (var i = 0; i < Model.Count; i += 3)
{
<tr>
@for (var j = i; j < Math.Min(i + 3, Model.Count); j++)
{
var propertyListing = Model[j];
<td class="property-cell">
<div class="property">
<a asp-action="Details" asp-route-id="@propertyListing.Id">
<div class="property-image">
@if (!string.IsNullOrEmpty(propertyListing.ImgPath))
{
<img src="@propertyListing.ImgPath" alt="Property Image" style="height: 330px; width: 330px;" />
}
</div>
<div class="overlay">
<div class="info">
<div class="tag price">₹ @propertyListing.AskingRate</div>
<h3>@propertyListing.Address1</h3>
<figure>@propertyListing.Address1</figure>
</div>
<ul class="additional-info">
<li>
<header>Area:</header>
<figure>@propertyListing.CovArea<sup>2</sup></figure>
</li>
<li>
<header>Beds:</header>
<figure>@propertyListing.BedroomNum</figure>
</li>
<li>
<header>Baths:</header>
<figure>@propertyListing.BathroomNum</figure>
</li>
</ul>
</div>
</a>
</div><!-- /.property -->
</td>
@if (j < Model.Count - 1)
{
<td class="property-cell spacer"></td>
}
}
</tr>
}
</table>
}
@{
var prevDisabled = !Model.HasPreviousPage ? "disabled" : "";
var nextDisabled = !Model.HasNextPage ? "disabled" : "";
}
<a asp-action="Show" asp-route-pageNumber="@(Model.PageIndex -1)" class="btn btn-default @prevDisabled">Prev</a>
<a asp-action="Show" asp-route-pageNumber="@(Model.PageIndex +1)" class="btn btn-default @nextDisabled">Next</a>
</div>
</section>
<aside id="advertising" class="block">
<div class="container">
<a asp-controller="Home" asp-action="Enquiry">
<div class="banner">
<div class="wrapper">
<span class="title">Do you want your property to be listed here?</span>
<span class="submit">Submit it now! <i class="fa fa-plus-square"></i></span>
</div>
</div><!-- /.banner-->
</a>
</div>
</aside><!-- /#adveritsing-->
在 HomeController 中我的表演
[HttpPost]
public IActionResult Show(int? city, int? serviceReqd, int? category, int? bedroom, decimal? minPrice, decimal? maxPrice, int ? pageNumber)
{
IQueryable<PropertyListWithImageViewModel> query;
else
{
// All fields are filled
//query = query.Where(x => x.CityId == city && x.CategoryId == category && x.BedroomNum == bedroom && x.ServiceId == serviceReqd);
query = from p in _context.PropertyListings
join s in _context.ServiceReqds on p.ServiceId equals s.ServiceId
join pc in _context.PropertyCategories on p.CategoryId equals pc.CategoryId
let firstPhoto = _context.ListingPhotos.Where(lp => lp.PropertyId == p.Id).OrderBy(lp => lp.PhotoId).FirstOrDefault()
where p.CategoryId == category && p.ServiceId == serviceReqd && p.CityId == city && p.BedroomNum == bedroom
select new PropertyListWithImageViewModel
{
Id = p.Id,
Address1 = p.Address1,
Address2 = p.Address2,
AskingRate = p.AskingRate,
CovArea = p.CovArea,
BedroomNum = p.BedroomNum,
BathroomNum = p.BathroomNum,
ServiceName = s.ServiceName,
PropertyCategoryName = pc.PropertyCategoryName,
ImgPath = firstPhoto != null ? firstPhoto.Imgpath : string.Empty // Select the first image for each property
};
}
}
}
}
// Materialize the query and project the results to PropertyListingViewModel
var results = query.ToList();
int pageSize = 5;
// Pass the results to the view
//return View(results);
return View(PaginatedList<PropertyListWithImageViewModel>.Create(results.ToList(), pageNumber ?? 1, pageSize));
}
my problem is when i click next to see the 6th record (default is 5) the next page is showing nothing (url-- https://localhost:7012/Home/Show?pageNumber=2) mean url is correct but no record is shown. I tried it using chatGPT and many other tools but it is showing. please help
我尝试使用 chatGPT、popAI 和 perplexity.ai 谷歌搜索,但无法纠正或追踪问题,我需要帮助
首先,如果您必须执行并具体化整个行集,那么分页列表类就有点毫无意义。我还会创建构造函数
private
以避免滥用并强制使用 Create() 工厂方法。一个简单的解决方法是:
private PaginatedList(List<T> items, int count, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
TotalPages = (int)Math.Ceiling(count/(double)pageSize);
this.AddRange(items);
}
public bool HasPreviousPage => PageIndex > 1;
public bool HasNextPage => PageIndex < TotalPages;
public static PaginatedList<T> Create (IQueryable<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);
}
这里我们给它一个 IQueryable,它可以检查它以获取计数,然后获取实际的单页结果。这意味着 EF 可以构建一个查询来返回 5 条记录,而不是执行一个查询来返回 all 记录,只是为了取出 5 条记录。
那么打电话时,不要这样做:
// var results = query.ToList();
只需用您的查询调用即可:
PaginatedList<PropertyListWithImageViewModel>.Create(query, pageNumber ?? 1, pageSize)
至于为什么您似乎没有获得第二页的任何结果,请使用断点并单步执行代码。 pageIndex 输入正确吗?调用 PaginatorList 类构造函数时会解析出什么内容?您的代码示例看起来是根据孤立的
else
语句进行切割的,因此不可能拼凑出可能发生的情况。逐步执行,这可能是一些愚蠢的事情,例如代码重新分配 pageIndex 而不是比较它,或者有条件地未设置 pageSize 并使用值 0 调用 Create。