没有参数MVC的分页列表分页

时间:2019-04-01 10:33:24

标签: c# asp.net-mvc razor pagedlist model-view

我在MVC项目中使用Pagedlist进行分页。分页适用于大多数没有ViewModels的情况。

但是当我使用ViewModel PagedList生成分页链接但没有任何参数值到querystring页面时。

这是我的ViewModel

public class SearchResult
{
    public int ItemID { get; set; }
    public string ItemTitle { get; set; }
    public int? AuthorID { get; set; }
    public string AuthorName { get; set; }  
    public int? IssueDate { get; set; }
}

和ActionResult方法

public ActionResult Date(int? page)
{
    try
    {
        string query;
        if (Request.RequestContext.RouteData.Values["query"] != null)
        {
            using (dreposEntities db = new dreposEntities())
            {
                query = Request.RequestContext.RouteData.Values["query"].ToString();
                int issueYear = int.Parse(query);
                ViewBag.Query = issueYear;

                var dateFilter = db.itemstreams.Where(q => q.issuedate == issueYear).ToList();
                List<SearchResult> resultList = new List<SearchResult>();
                if (dateFilter.Any())
                {
                    foreach (var c in dateFilter)
                    {
                        SearchResult obj = new SearchResult
                        {
                            ItemID = c.id,
                            ItemTitle = c.title,
                            IssueDate = c.issuedate,
                        };
                        resultList.Add(obj);
                    }
                }
                ViewBag.SearchQuery = query;

                return View(resultList.OrderBy(d=>d.IssueDate).ToPagedList(page ?? 1, 10));
            }
        }
        else
        {
            using (dreposEntities db = new dreposEntities())
            {
                List<SearchResult> resultList = new List<SearchResult>();
                var files = db.itemstreams.Where(s=>s.status==1).ToList();
                if (files.Any())
                {
                    foreach (var f in files)
                    {
                        SearchResult obj = new SearchResult
                        {
                            ItemID = f.id,
                            ItemTitle = f.title,
                            IssueDate = f.issuedate,
                        };
                        resultList.Add(obj);
                    }
                }
                return View(resultList.OrderBy(d=>d.IssueDate).ToPagedList(page ?? 1, 10));
            }
        }
    }
    catch (Exception e)
    {
        return View();
    }
}

上面的代码在View中显示的内容附在下面的电子邮件中 enter image description here

在视图/剃刀中

<div class="pull-right">
                    @Html.PagedListPager(Model, page => Url.Action("Date", new { page }), new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded })
                </div>

0 个答案:

没有答案