c# - 您如何通过 ASP.net 核心中的 IEnumerable ViewModel 进行分页?
问题描述
我正在尝试将分页添加到 asp.net 中的视图。这是我尝试将分页添加到我创建的视图的地方。当页面仅包含 @Model ViewModel 行时,我的动态分页有效。我还没有看到任何关于如何对这个特定的东西进行分页的信息,我也没有找到太多关于它的信息。是否有明显的遗漏或者我需要重写我的代码来添加分页。
这是问题发生位置的链接。我得到的错误是 CS1061“IEnumerable 不包含 PageNUMeringInfo 的定义。
@model IEnumerable<byudigs.Models.ViewModels.ViewModel>
<link href="~/css/Table.css" rel="stylesheet" />
<br />
<br />
<br />
<br />
<section class="get-in-touch">
<h1 class="title">Mummy Finder</h1>
<div class="container py-5">
@*<header class="text-center text-white">
<h1 class="display-4">Bootstrap Datatables</h1>
<p class="lead mb-0">
Using Bootstrap 4 and <a href="https://datatables.net/examples/styling/bootstrap4.html" class="text-white font-italic">
<u>Datatables</u>
</a>, add interaction controlsto your HTML tables.
</p>
<p class="font-italic">
Snippet By
<a href="https://bootstrapious.com" class="text-white">
<u>Bootstrapious</u>
</a>
</p>
</header>*@
@*<input name="searchString" />*@
<div class="row py-5">
<div class="col-lg-10 mx-auto">
<div class="card rounded shadow border-0">
<div class="card-body p-5 bg-white rounded">
<div class="table-responsive">
<table class="table table-striped table-bordered">
<thead>
<tr>
<!--This is creating the headers of the table-->
<th style="padding-left:8px; padding-right:8px;">Location:</th>
<th style="padding-left:8px; padding-right:8px;">South to Head:</th>
<th style="padding-left:8px; padding-right:8px;">South to Feet:</th>
<th style="padding-left:8px; padding-right:8px;">West to Head:</th>
<th style="padding-left:8px; padding-right:8px;">West to Feet:</th>
<th style="padding-left:8px; padding-right:8px;">Length:</th>
<th style="padding-left:8px; padding-right:8px;">Burial Depth:</th>
<th style="padding-left:8px; padding-right:8px;">Hair:</th>
<th style="padding-left:8px; padding-right:8px;">Gender:</th>
<th style="padding-left:8px; padding-right:8px;">Head Direction:</th>
<th style="padding-left:8px; padding-right:8px;">Artifact Found?</th>
<th style="padding-left:8px; padding-right:8px;">Date Discovered</th>
</tr>
</thead>
<tbody>
@foreach (var x in Model)
{
<tr>
<!--This is assigning the model variables to the list so that it can be seen in this view-->
<td style="padding-left:8px; padding-right:8px;"> @x.burialadvanced.HairColor @x.burial.BurialNum @x.plot.BurialLocationNs @x.plot.LowPairNs/@x.plot.HighPairNs @x.plot.BurialLocationEw @x.plot.LowPairEw/@x.plot.HighPairEw @x.sublocation.Direction #@x.burial.BurialNum @x.burial.BurialSubnum</td>
<td style="padding-left:8px; padding-right:8px;">@x.burialadvanced.SouthToHead</td>
<td style="padding-left:8px; padding-right:8px;">@x.burialadvanced.SouthToFeet</td>
<td style="padding-left:8px; padding-right:8px;">@x.burialadvanced.EastToHead</td>
<td style="padding-left:8px; padding-right:8px;">@x.burialadvanced.EastToFeet</td>
<td style="padding-left:8px; padding-right:8px;">@x.burialadvanced.LengthOfRemains</td>
<td style="padding-left:8px; padding-right:8px;">@x.burialadvanced.BurialDepth</td>
<td style="padding-left:8px; padding-right:8px;">@x.burialadvanced.HairColor</td>
<td style="padding-left:8px; padding-right:8px;">@x.burialadvanced.GenderBodyCol</td>
<td style="padding-left:8px; padding-right:8px;">@x.burialadvanced.HeadDirection</td>
<th style="padding-left:8px; padding-right:8px;">@x.burialadvanced.ArtifactFound</th>
<th style="padding-left:8px; padding-right:8px;">@x.burialadvanced.MonthFound/@x.burialadvanced.DayFound/@x.burialadvanced.YearFound</th>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<!--paginated links-->
<div page-info="@Model.PageNumberingInfo"
page-action="BurialList"
page-classes-enabled="true"
page-class="btn"
page-class-normal="btn-outline-secondary"
page-class-selected="btn-primary">
</div>
}
这是我的控制器的 BurialList 部分。
[AllowAnonymous]
public IActionResult BurialList(string searchString, int pageNum=1)
{
ViewBag.Burial = _context.Burial;
ViewBag.BurialAdvanced = _context.BurialAdvanced;
ViewBag.Sublocation = _context.Sublocation;
ViewBag.Date = _context.Date;
//var burials = _context.BurialAdvanced.FromSqlRaw("SELECT * from burial_advanced ba join burial b on ba.burial_id = b.burial_id join plot p on b.plot_id = p.plot_id join sublocation s on b.sublocation_id = s.sublocation_id");
//Filter by hair color
if (!String.IsNullOrEmpty(searchString))
{
List<Burial> burial = _context.Burial.ToList();
List<Plot> plot = _context.Plot.ToList();
List<Sublocation> sublocation = _context.Sublocation.ToList();
List<BurialAdvanced> burialadvanced = _context.BurialAdvanced.Where(x => x.HairColor.Contains(searchString)).ToList();
var burialRecord = from ba in burialadvanced
join b in burial on ba.BurialId equals b.BurialId into table1
from b in table1.ToList()
join p in plot on b.PlotId equals p.PlotId into table2
from p in table2.ToList()
join s in sublocation on b.SublocationId equals s.SublocationId into table3
from s in table3.ToList()
select new ViewModel
{
burial = b,
burialadvanced = ba,
plot = p,
sublocation = s,
PageNumberingInfo = new PageNumberingInfo
{
CurrentPage = pageNum,
NumItemsPerPage = PageSize,
TotalNumItems = 500
//TotalNumItems = (burialId == null ? _repository.Burials.Count() :
// _repository.Burials.Where(b => b.BurialId == burialId).Count())
},
};
return View(burialRecord);
}
else
{
List<Burial> burial = _context.Burial.ToList();
List<Plot> plot = _context.Plot.ToList();
List<Sublocation> sublocation = _context.Sublocation.ToList();
List<BurialAdvanced> burialadvanced = _context.BurialAdvanced.ToList();
var burialRecord = from ba in burialadvanced
join b in burial on ba.BurialId equals b.BurialId into table1
from b in table1.ToList()
join p in plot on b.PlotId equals p.PlotId into table2
from p in table2.ToList()
join s in sublocation on b.SublocationId equals s.SublocationId into table3
from s in table3.ToList()
select new ViewModel
{
burial = b,
burialadvanced = ba,
plot = p,
sublocation = s
};
return View(burialRecord);
}
//return View(burialRecord.ToList());
}
这是我的 PaginationTagHelper 类,位于我的基础结构文件夹中
namespace byudigs.Infrastructure
{
[HtmlTargetElement("div", Attributes = "page-info")]
public class PaginationTagHelper : TagHelper
{
private IUrlHelperFactory urlHelperFactory;
public PaginationTagHelper(IUrlHelperFactory uhf)
{
urlHelperFactory = uhf;
}
//public string TeamName { get; set; }
//our own key value dictionary for team name and paging info
[HtmlAttributeName(DictionaryAttributePrefix = "page-url-")]
public Dictionary<string, object> KeyValuePairs { get; set; } = new Dictionary<string, object>();
[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }
public PageNumberingInfo PageInfo { get; set; }
public string PageAction { get; set; }
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 finishedTag = new TagBuilder("div");
//loop that will append a link for every page
for (int i = 1; i <= PageInfo.NumPages; i++)
{
TagBuilder individualTag = new TagBuilder("a");
if (PageClassesEnabled)
{
//add Css capabilities if the page exists
individualTag.AddCssClass(PageClass);
individualTag.AddCssClass(i == PageInfo.CurrentPage ? PageClassSelected : PageClassNormal);
}
KeyValuePairs["pageNum"] = i;
individualTag.Attributes["href"] = urlHelper.Action("BurialList", KeyValuePairs);
individualTag.InnerHtml.Append(i.ToString());
finishedTag.InnerHtml.AppendHtml(individualTag);
}
output.PostContent.AppendHtml(finishedTag.InnerHtml);
}
}
}
这是我的 PageNumberingInfo 视图模型
namespace byudigs.Models.ViewModels
{
public class PageNumberingInfo
{
//info to get from controller, pass to view as defaults for the structure of the page
public int NumItemsPerPage { get; set; }
public int CurrentPage { get; set; }
public int TotalNumItems { get; set; }
//calculate pages
//cast one of the properties as a decimal to force decimal division
public int NumPages => (int)Math.Ceiling((decimal)TotalNumItems / NumItemsPerPage);
}
}
这是我的视图模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace byudigs.Models.ViewModels
{
public class ViewModel
{
public Burial burial { get; set; }
public BurialAdvanced burialadvanced { get; set; }
public Plot plot { get; set; }
public Sublocation sublocation { get; set; }
public PageNumberingInfo PageNumberingInfo { get; set; }
}
}
解决方案
在选择新 ViewModel { ... } 之前执行此操作:
skip (pageNum - 1) * pageSize
take pageSize
编辑
因此,您将要编辑 ViewModel 类并为个人埋葬记录创建一个新类。
public class ViewModel {
PageNumberingInfo PageNumberingInfo { get; set; },
BurialRecord BurialRecord { get; set; }
}
public class BurialRecord {
Burial Burial { get; set; }
BurialAdvanced BurialAdvanced { get; set; }
Plot Plot { get; set; }
Sublocation Sublocation {get;set;
}
现在在您的控制器中:
var burialRecords = from ba in burialadvanced
join b in burial on ba.BurialId equals b.BurialId into table1
from b in table1.ToList()
join p in plot on b.PlotId equals p.PlotId into table2
from p in table2.ToList()
join s in sublocation on b.SublocationId equals s.SublocationId into table3
from s in table3.ToList()
select new BurialRecord
{
burial = b,
burialadvanced = ba,
plot = p,
sublocation = s,
};
var model = new ViewModel
{
PageNumberingInfo = new PageNumberingInfo
{
CurrentPage = pageNum,
NumItemsPerPage = PageSize,
TotalNumItems = 500
},
BurialRecords = burialRecords
};
推荐阅读
- java - 我们通过将 maven 依赖 jar 版本从 5.1.6 升级到 8.0.2,在四个不同的客户端环境上执行 mysql 爬取
- typescript - 打字稿句柄事件定义
- ios - fileManager.contentsOfDirectory 返回一个空数组
- node.js - npm install uuidv4 ,在 Windows 中,返回“无效版本”
- javascript - ng-if 检查 ng-repeat 中的变量
- distributed-computing - 有关小规模工作流程的真实实验的详细信息
- sql-server - 处理 248 条消息后来自 SQL Server 的 HTTP 请求关闭连接
- javascript - 如果我使用了 json_encode(),是否需要 json.parse?
- react-native - React Navigation v5 Auth 与 Apollo 客户端不工作
- python - 在 Python 中计算累积分布函数的简单方法