![](/img/trans.png)
[英]ASP.NET Core - Bind IEnumerable<T> to a ViewModel field on POST
[英]How do you paginate through an IEnumerable ViewModel in ASP.net core?
我正在尝试向 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>
}
这是我的 controller 的 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 class 位于我的基础设施文件夹中
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 视图 model
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; }
}
}
在 select new ViewModel {... } 之前执行此操作:
skip (pageNum - 1) * pageSize
take pageSize
编辑
因此,您将要编辑您的 ViewModel class 并为个人埋葬记录创建一个新的 class。
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;
}
现在在您的 controller 中:
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
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.