【问题标题】:PagedList in MVC3 showing Error Object reference not set to an instance of an objectMVC3 中的 PagedList 显示错误对象引用未设置为对象的实例
【发布时间】:2014-11-13 12:14:14
【问题描述】:

这是我的观点

@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"),

我已经浏览了一些链接,我可以通过这些链接编写这样的代码,最后卡在这里 对此的任何帮助都非常感谢..

【问题讨论】:

  • Request.QueryString["Something"] 可以一直是 null 如果它在请求 url 中不存在,并且当您尝试执行 ToLower()null 时会导致异常,您有对它们进行空值检查,或者找出一种方法来避免当您需要的东西为空时视图在控制器中呈现。
  • 每次我看到该错误都是由于使用了未实例化的列表。确保您已对所有列表执行此操作。
  • 使用 ViewBag 将各种参数传递给PagedListPager。计算控制器中的值,并且在视图中没有复杂的逻辑。从查询字符串中提取参数,当控制器具有强类型值时,是一种浪费。

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 razor pagedlist


【解决方案1】:

使用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 条记录目前很慢

从下面的 cmets 当前分页很慢。这是因为下一行中的 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);

【讨论】:

  • TrueBlueAussie 和@tweray 你是对的。将值存储在 ViewBag 中就可以了。非常感谢你的帮助。
  • @user3759894:PagedList 非常有用,但是您需要一个很好的示例来遵循实现模式。我从 GitHub 源代码 (github.com/troygoode/PagedList) 上显示的基本模式开始我的,并不断扩展它。
  • 谢谢@TrueBlueAussie ...目前我正在执行搜索的记录大约有 10000 条,有什么方法可以让 pagedList 更快。最近我与与分页列表相比,webgrid 速度稍快...但是我想以提高速度的方式使用分页列表...有什么办法吗?
  • 在查询所有 10,000 条记录然后创建分页列表时,您需要摆脱 ToList()。 ToPagedList 将添加 skip(n)take(n) 所以它需要是一个 IQueryable(不是列表)。
  • 对不起@TrueBlueAussie,我是 mvc 新手,这就是我在数据层中所做的,此方法检索 10,000 条记录,我正在对返回的记录执行搜索。你想让我怎么处理它..?下面是代码 public List GetBlogSet(string type) { List Obj= _dbContext.Usp_getBlogSetPosts(type.ToString()).ToList();返回对象; }
猜你喜欢
  • 1970-01-01
  • 2012-02-03
  • 2014-04-11
  • 2011-07-09
  • 2012-08-31
  • 2011-11-21
  • 2012-10-01
  • 1970-01-01
相关资源
最近更新 更多