Asp.net mvc MVC3中的PagedList显示错误对象引用未设置为对象实例
这是我的观点Asp.net mvc MVC3中的PagedList显示错误对象引用未设置为对象实例,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-4,razor,pagedlist,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 4,Razor,Pagedlist,这是我的观点 @using(@Html.BeginForm("CrmBlogGroupType","knowledge",FormMethod.Get)){ @Html.TextBox("search") @Html.Hidden("type", (string)ViewBag.type) @Html.DropDownList("PageSize", new List<SelectListItem>()
@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>
}
控制器代码
@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);
}
我已经通过了一些链接,通过这些链接我可以编出这样的代码,最后被困在这里
非常感谢您在这方面提供的任何帮助。使用
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
等
更新:10000条记录当前速度较慢
@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>
}
从下面的注释可以看出,当前分页速度很慢。这是因为下一行中的ToList()
导致在对LINQ查询应用任何分页之前返回所有记录
Request.QueryString[“Something”]
如果在请求url中不存在,则它始终可以是null
,并且当您尝试执行ToLower()
到null
时,它将导致异常,您必须对它们执行null检查,或者找出一种方法来避免在需要的东西为空时在控制器中呈现视图。每次我看到这个错误都是因为使用了一个未实例化的列表。确保对所有列表都这样做。使用ViewBag将各种参数传递到PagedListPager
。计算控制器中的值,并且在视图中没有复杂的逻辑。当控制器具有强类型值时,从querystring中提取参数是一种浪费。。将值存储在ViewBag中实现了这一点。。非常感谢您的帮助。@user3759894:PagedList非常有用,但是您需要一个很好的示例来遵循实现模式。我从GitHub源代码()上显示的基本模式开始,并不断扩展它。。。目前,我正在搜索大约10000条记录,是否有任何方法可以让页面列表更快。。最近我使用了webgrid,它比分页列表快一点。。。然而,我想用改进的速度与页面列表坚持。。。有什么办法吗?在查询所有10000条记录,然后创建分页列表时,需要去掉ToList()
。ToPagedList将添加skip(n)
和take(n)
,因此它需要是一个IQueryable(不是列表)。抱歉@TrueBlueAusie我是mvc的新手,这是我在数据层中所做的,此方法检索10000条记录,我正在对返回的记录执行搜索。你想让我怎么处理这件事。。?下面是代码公共列表GetBlogSet(字符串类型){List Obj=_dbContext.Usp_getBlogSetPosts(type.ToString()).ToList();return Obj;}
IEnumerable<Usp_getBlogSetPosts_Result> _objBlogSet =
_dataLayer.GetBlogSet(type)
.ToList() // <<<< THIS IS THE CULPRIT
.ToPagedList(page ?? 1, PageSize ?? 10);
IEnumerable<Usp_getBlogSetPosts_Result> _objBlogSet =
_dataLayer.GetBlogSet(type)
.ToPagedList(page ?? 1, PageSize ?? 10);