这是我的观点
@using(@Html.BeginForm("CrmBlogGroupType","knowledge",FormMethod.Get)){
@Html.TextBox("search")
@Html.Hidden("type", (string)ViewBag.type)
@Html.DropDownList("PageSize",
new List<SelectListItem>()
{
new SelectListItem ()
{
Text="--Select Page Size--" ,Value="10",Selected=true
},
new SelectListItem ()
{
Text="View 20 records" ,Value="20"
},
new SelectListItem ()
{
Text="View 50 records" ,Value="50"
},
new SelectListItem ()
{
Text="View 100 records" ,Value="100"
},
})
<input type="submit" value="search" id="Searchbtn" />
<br />
@Html.CheckBox("Name")<text>Author Name</text>
@Html.CheckBox("AuthorTitle")<text>Title</text>
@Html.CheckBox("Description")<text>Description</text>
}
以下是PagedList代码
@Html.PagedListPager(Model, page => Url.Action("CrmBlogGroupType",
new {page,Name=Request.QueryString["Name"].ToLower().Contains("true"),
AuthorTitle=Request.QueryString["AuthorTitle"].ToLower().Contains("true"),
Description=Request.QueryString["Description"].ToLower().Contains("true"), search=Request.QueryString["search"],PageSize=Request.QueryString["PageSize"],type=Request.QueryStrin g["type"]}),new PagedListRenderOptions()
{
DisplayLinkToFirstPage=true,DisplayLinkToLastPage=true,DisplayPageCountAndCurrentLocation=true,Displa yItemSliceAndTotal=true
,DisplayEllipsesWhenNotShowingAllPageNumbers=true,MaximumPageNumbersToDisplay=10
})
控制器代码
public ActionResult CrmBlogGroupType(int? page, bool? Name, bool? AuthorTitle, bool?Description, string search, int? PageSize, string type)
{
if (type==null)
{
//setting the Value in the initial call
//If the SP has changed then make the type parameter as the INT
type = "A";
}
IEnumerable<Usp_getBlogSetPosts_Result> _objBlogSet = _dataLayer.GetBlogSet(type).ToList().ToPagedList(page ?? 1, PageSize ?? 10);
return View(_objBlogSet);
}
获取错误:
对象引用未设置为对象的实例。
Line 202: @if (ViewBag.Search!=null && ViewBag.Search!=string.Empty)
Line 203:{
Line 204:@Html.PagedListPager(Model, page => Url.Action("CrmBlogGroupType", new { page,
Line 205:Name=Request.QueryString["Name"].ToLower().Contains("true"),AuthorTitle=Request.QueryString["Auth orTitle"].ToLower().Contains("true"),
Line 206:Description=Request.QueryString["Description"].ToLower().Contains("true"),
我已经浏览了一些链接,我可以像这样编写代码,最后卡在这里 对此的任何帮助都非常感谢..
答案 0 :(得分:0)
使用ViewBag
将各种参数传递给PagedListPager
。计算控制器中的值,并且视图中没有复杂的逻辑。当控制器为那些强类型值时,从querystring
,中提取参数是不必要的重复工作。
public ActionResult CrmBlogGroupType(int? page, bool? Name, bool? AuthorTitle, bool?Description, string search, int? PageSize, string type)
{
// Get the current values (or defaults == false) for the sorting
ViewBag.Name = Name.GetValueOrDefault();
ViewBag.AuthorTitle = AuthorTitle.GetValueOrDefault();
ViewBag.Description= Description.GetValueOrDefault();
并在视图中使用它们:
@Html.PagedListPager(Model, page => Url.Action("CrmBlogGroupType",
new {page, Name=ViewBag.Name, AuthorTitle=ViewBag.AuthorTitle, Description=ViewBag.Description
等
更新:10,000条记录目前很慢
从下面的评论中,当前的分页很慢。这是因为以下行中的ToList()
导致在将任何分页应用于LINQ查询之前返回所有记录。
IEnumerable<Usp_getBlogSetPosts_Result> _objBlogSet =
_dataLayer.GetBlogSet(type)
.ToList() // <<<< THIS IS THE CULPRIT
.ToPagedList(page ?? 1, PageSize ?? 10);
ToPagedList
旨在使用IQueryable
,以便在向查询添加Skip(n)
和Take(n)
时,它将有效地返回页面值记录。只需删除ToList()
:
IEnumerable<Usp_getBlogSetPosts_Result> _objBlogSet =
_dataLayer.GetBlogSet(type)
.ToPagedList(page ?? 1, PageSize ?? 10);