我正在尝试使用DbContext
在页面中显示我的数据。
这是我的控制器:
public async Task<IActionResult> Index(int? page)
{
int _page;
if (page.HasValue)
{
_page = page.Value;
}
else
_page = 1;
return View(await _context.Agent.Skip(_page-1).Take(10).ToListAsync());
我的布局页面包含链接:
<li><a asp-area="" asp-controller="Agents" asp-action="Index" asp-route-id="1" >סוכן</a></li>
两件事:
A)它似乎不起作用,无论我在asp-route-id
中传递的是同一个列表。我做错了什么?
B)工作完成后,如何呈现供用户选择的页面? (E.G.1,2,3..90)
感谢。
答案 0 :(得分:5)
您的参数名为page
,但您提供了id
路由值。将操作方法中的page
参数重命名为id
,它应该可以正常工作。
答案 1 :(得分:2)
或者为下面的视图编写自己的分页逻辑实现(主要基于official documentation),不是完美的分页实现,但仍在工作:
页面模型:
public class PageViewModel
{
public int PageNumber { get; }
public int TotalPages { get; }
public PageViewModel(int count, int pageNumber, int pageSize)
{
PageNumber = pageNumber;
TotalPages = (int)Math.Ceiling(count / (double)pageSize);
}
public bool HasPreviousPage => (PageNumber > 1);
public bool HasNextPage => (PageNumber < TotalPages);
}
代码帮助程序:
public class PagerTagHelper : TagHelper
{
private IUrlHelperFactory urlHelperFactory;
public PagerTagHelper(IUrlHelperFactory helperFactory)
{
urlHelperFactory = helperFactory;
}
[ViewContext]
[HtmlAttributeNotBound]
public ViewContext ViewContext { get; set; }
public PageViewModel PageModel { get; set; }
public string PageAction { get; set; }
public string PageController { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
IUrlHelper urlHelper = urlHelperFactory.GetUrlHelper(ViewContext);
output.TagName = "div";
// we put our links in list
TagBuilder tag = new TagBuilder("ul");
tag.AddCssClass("pagination");
//show link for 1st page
if (PageModel.PageNumber>2)
{
TagBuilder fstItem = CreateTag(1, urlHelper);
tag.InnerHtml.AppendHtml(fstItem);
}
// create 3 links to current, next and previous page.
TagBuilder currentItem = CreateTag(PageModel.PageNumber, urlHelper);
// link to previous page if NOT 1st page
if (PageModel.HasPreviousPage)
{
TagBuilder prevItem = CreateTag(PageModel.PageNumber - 1, urlHelper);
tag.InnerHtml.AppendHtml(prevItem);
}
tag.InnerHtml.AppendHtml(currentItem);
// link to next page, if NOT last page
if (PageModel.HasNextPage)
{
TagBuilder nextItem = CreateTag(PageModel.PageNumber + 1, urlHelper);
tag.InnerHtml.AppendHtml(nextItem);
}
//show code for last page
if (PageModel.TotalPages > 4 && PageModel.PageNumber!=PageModel.TotalPages-1 && PageModel.HasNextPage)
{
TagBuilder lstItem = CreateTag(PageModel.TotalPages, urlHelper);
tag.InnerHtml.AppendHtml(lstItem);
}
output.Content.AppendHtml(tag);
}
TagBuilder CreateTag(int pageNumber, IUrlHelper urlHelper)
{
TagBuilder item = new TagBuilder("li");
TagBuilder link = new TagBuilder("a");
if (pageNumber == this.PageModel.PageNumber)
{
item.AddCssClass("active");
}
else
{
link.Attributes["href"] = urlHelper.Action(PageAction, PageController, new { page = pageNumber });
}
link.InnerHtml.Append(pageNumber.ToString());
item.InnerHtml.AppendHtml(link);
return item;
}
}
并在View中使用它(不要忘记在_ViewImports.cshtml中导入tag-helper ):
<pager page-model="@Model.PageViewModel" page-action="Index" page-controller="Home"></pager>
控制器的代码:
public async Task<IActionResult> Index(int page=1)
{
int pageSize = 10;
var count = await _context.Agent.CountAsync();
var items = await _context.Agent.Skip((page - 1) * pageSize).Take(pageSize).ToListAsync();
PageViewModel pageVm= new PageViewModel(count, page, pageSize);
IndexViewModel vm= new IndexViewModel
{
PageViewModel = pageVm,
Agents =items // need to create table in view from Agent properties
};
return View(vm);
}
IndexViewModel的代码:
public class IndexViewModel
{
public IEnumerable<Agent> Agents { get; set; }
public PageViewModel PageViewModel { get; set; }
}
<强>更新强>
如果您想在视图中显示所有PageLinks,您可以在Tag Helper Process
方法中使用for循环,如下所示:
public override void Process(TagHelperContext context, TagHelperOutput output)
{
IUrlHelper urlHelper = urlHelperFactory.GetUrlHelper(ViewContext);
output.TagName = "div";
// we put our links in list
TagBuilder tag = new TagBuilder("ul");
tag.AddCssClass("pagination");
for (int i = 1; i <= PageModel.TotalPages; i++)
{
TagBuilder linkItem = CreateTag(i, urlHelper);
tag.InnerHtml.AppendHtml(linkItem);
}
output.Content.AppendHtml(tag);
}
更新2 :
为了使用这种方法,不要忘记将索引页的模型更改为IndexViewModel
并在_ViewImports.cshtml中注册标记帮助程序。我的工作视图(简化)看起来像这样。
@* Using and tag helper imported in _ViewImports.cshtml *@
@model IndexViewModel
@{
ViewData["Title"] = "Home";
}
<div>
<table class="table">
<tr><th>Name</th><th>Age</th></tr>
@foreach (var a in Model.Agents)
{
<tr><td>@a.Name</td><td>@a.Age</td></tr>
}
</table>
</div>
<pager page-model="@Model.PageViewModel" page-action="Index" page-controller="Home"></pager>
更新3 :
IndexViewModel
,但不像我的示例那样使用foreach,则应将@Html.DisplayNameFor(model => model.Name)
更改为@Html.DisplayNameFor(model => model.Agents.Name)
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
仅导入内置的ASP.NET Core标记帮助程序。要导入自定义代码,您应该使用@addTagHelper *, YourAssemblyName
,其中YourAssemblyName
是您项目的名称(例如,代理商代理商),有关详细信息,请参阅A minimal Tag Helper section in documentation。