我想在我的应用程序中实现pagination
。
我的控制器
public ActionResult Leading(string type, int? pageNumber)
{
IEnumerable<LeadingClass> data;
LeadingDatabaseHandle DatabaseHandle = new LeadingDatabaseHandle ();
if (type.HasValue)
{
data = DatabaseHandle(type).ToPagedList(pageNumber ?? 1, 10);
}
else
{
data = DatabaseHandle.LeadingAll().ToPagedList(pageNumber ?? 1, 10);
}
LeadingFilterVM model = new LeadingFilterVM
{
Type = type,
TypeList = new List<SelectListItem>
{
new SelectListItem{ Text = "General", Value = "1" },
new SelectListItem{ Text = "Advance", Value = "2" }
},
Leadings = data
};
return View(model);
}
我的数据模型
public class LeadingClass
{
public int Id { get; set; }
public string Name{ get; set; }
public string Type { get; set; }
}
我的视图模型
public class LeadingFilterVM
{
public string Type { get; set; }
public IEnumerable<SelectListItem> TypeList { get; set; }
public IEnumerable<LeadingClass> Leadings { get; set; }
}
我的观点
@model Website.Models.Leading.ViewModels.LeadingFilterVM
@using PagedList;
@using PagedList.Mvc;
@using (Html.BeginForm("Leading", "Home", FormMethod.Get))
{
@Html.DisplayNameFor(m => m.Leadings.FirstOrDefault().Type)
@Html.DropDownListFor(m => m.Type, Model.TypeList, "All Type", new { @class = "form-control"})
}
<table class="table table-hover">
<tr>
<th>@Html.DisplayNameFor(m => m.Leadings.FirstOrDefault().Id)</th>
<th>@Html.DisplayNameFor(m => m.Leadings.FirstOrDefault().Name)</th>
</tr>
@foreach (var item in Model.Leadings)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.Id)</td>
<td>@Html.DisplayFor(modelItem => item.Name)</td>
</tr>
}
</table>
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, pageNumber => Url.Action("Leading", new
{
pageNumber
}))
抛出错误:编译器错误消息:
CS1061:&#39; LeadingFilterVM&#39;不包含&#39; PageNumber&#39;的定义没有扩展方法&#39; PageNumber&#39;接受第一个类型&#39; LeadingFilterVM&#39;可以找到(你错过了使用指令或程序集引用吗?)
有人可以指导我吗?
答案 0 :(得分:1)
视图中的模型为LeadingFilterVM
,该模型不包含PageNumber
的属性。
请注意,您的代码还存在其他问题,您需要将视图模型更改为
public class LeadingFilterVM
{
public string Type { get; set; }
public IEnumerable<SelectListItem> TypeList { get; set; }
public IPagedList<LeadingClass> Leadings { get; set; } //IPagedList, not IEnumerable
}
然后您需要更改视图以使用Leadings
IPagedList
属性
Page @(Model.Leadings.PageCount < Model.Leadings.PageNumber ? 0 : Model.Leadings.PageNumber) of @Model.Leadings.PageCount
@Html.PagedListPager(Model.Leadings, page => Url.Action("Leading", new { page, type = Model.Type }))
并将方法的签名更改为
public ActionResult Leading(int? type, int? pageNumber)
您还需要更改代码以生成SelectList
到
TypeList = new List<SelectListItem>
{
new SelectListItem{ Text = "General", Value = "General" },
new SelectListItem{ Text = "Advance", Value = "Advance" }
},
或只是TypeList = new SelectList(new string[]{ "General", "Advance" })
,因为您需要回复&#34; General&#34;或者&#34;提前&#34;而不是int
。话虽如此,我建议Type
在int
中,与数据库中的Types
表有FK关系。
请注意,不清楚您的DatabaseHandle.LeadingAll()
和DatabaseHandle(type)
方法会返回什么,但它们应该退回IQueryable<LeadingClass>
(不是IEnumerable<>
或IList<>
)否则您会失败服务器端分页的好处。获取记录的典型代码是
public ActionResult Leading(string type, int? pageNumber)
{
IQueryable<LeadingClass> data = db.LeadingClass;
if (type != null)
{
data = data.Where(x => x.Type == type.Value);
}
LeadingFilterVM model = new LeadingFilterVM
{
....
Leadings = data..ToPagedList(pageNumber ?? 1, 10)
};
return View(model);
}