【发布时间】:2021-02-02 18:41:12
【问题描述】:
我正在尝试在我的项目中修复分页。但我没有得到正确的结果。我想让这个结果 123 ... 12
PS 我用 Adam Freeman Pro ASP.NET Core MVC 2 书学习。 GitHub中的项目https://github.com/JasARGHUN/StoreProject-
分页模型 PagingInfo.cs:
public class PagingInfo
{ public int TotalItems { get; set; }
public int ItemsPerPage { get; set; }
public int CurrentPage { get; set; }
public int TotalPages =>
(int)Math.Ceiling((decimal)TotalItems / ItemsPerPage); }
描述符类PageLinkTagHelper.cs
[HtmlTargetElement("div", Attributes = "page-model")]
public class PageLinkTagHelper : TagHelper
{
private IUrlHelperFactory urlHelperFactory;
public PageLinkTagHelper(IUrlHelperFactory helperFactory)
{
urlHelperFactory = helperFactory;
}
[ViewContext]
[HtmlAttributeNotBound]
public ViewContext ViewContext { get; set; }
public PagingInfo PageModel { get; set; }
public string PageAction { get; set; }
[HtmlAttributeName(DictionaryAttributePrefix = "page-url-")]
public Dictionary<string, object> PageUrlValues { get; set; } = new Dictionary<string, object>();
public bool PageClassesEnabled { get; set; } = false;
public string PageClass { get; set; }
public string PageClassNormal { get; set; }
public string PageClassSelected { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
IUrlHelper urlHelper = urlHelperFactory.GetUrlHelper(ViewContext);
TagBuilder result = new TagBuilder("div");
for (int i = 1; i <= PageModel.TotalPages; i++)
{
TagBuilder tag = new TagBuilder("a");
PageUrlValues["productPage"] = i;
tag.Attributes["href"] = urlHelper.Action(PageAction, PageUrlValues);
if (PageClassesEnabled)
{
tag.AddCssClass(PageClass);
tag.AddCssClass(i == PageModel.CurrentPage
? PageClassSelected : PageClassNormal);
}
tag.InnerHtml.Append(i.ToString());
result.InnerHtml.AppendHtml(tag);
}
output.Content.AppendHtml(result.InnerHtml);
}
}
分页视图模型 ProductsListViewModel
public class ProductsListViewModel
{
public IEnumerable<Product> Products { get; set; }
public PagingInfo PagingInfo { get; set; }
public string CurrentCategory { get; set; }
}
控制器 ProductController.cs
public class ProductController : Controller
{
private IProductRepository _repository;
public int PageSize = 4;
public ProductController(IProductRepository repository)
{
_repository = repository;
}
public ViewResult List(string category, int productPage = 1) =>
View(new ProductsListViewModel
{
Products = _repository.Products
.Where(p => category == null || p.Category == category)
.OrderBy(p => p.ProductID)
.Skip((productPage - 1) * PageSize)
.Take(PageSize),
PagingInfo = new PagingInfo
{
CurrentPage = productPage,
ItemsPerPage = PageSize,
TotalItems = category == null ?
_repository.Products.Count() :
_repository.Products.Where(e =>
e.Category == category).Count()
},
CurrentCategory = category
});}
查看 List.cshtml
@model ProductsListViewModel
@foreach (var p in Model.Products)
{
@await Html.PartialAsync("ProductSummary", p);
}
<div page-model="@Model.PagingInfo" page-action="List" page-classes-enabled="true"
page-class="btn" page-class-normal="btn-secondary"
page-class-selected="btn-primary" page-url-category="@Model.CurrentCategory"
class="btn-group pull-right m-1"></div>
导航菜单 Navigation.cshtml
@model IEnumerable<string>
<a class="btn btn-block btn-secondary border mb-1"
asp-action="List"
asp-controller="Product"
asp-route-category="">Home</a>
<div>
@foreach (string category in Model)
{
<a class="btn btn-sm btn-block border @(category == ViewBag.SelectedCategory ? "btn-info": "btn-light")"
asp-action="List"
asp-controller="Product"
asp-route-category="@category"
asp-route-productPage="1">@category</a>
}
</div>
【问题讨论】:
-
您能解释一下您希望看到的结果吗?
-
谢谢,我希望分页为 123...12,而不是 1 2 3 4 5 6 7 8 9 10 11 12
标签: c# asp.net-core razor