(作业)MVC分页帮助 [英] (Homework) MVC Pagination Help

查看:99
本文介绍了(作业)MVC分页帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图放在一起使用ASP.NET MVC一个非常简单的应用程序,显示新闻报道和进行分页他们。我是那种中途有,但需要一些帮助整理出分页和得到它与搜索查询工作。

I'm attempting to put together a very simple application using ASP.NET MVC that shows news articles and paginates them. I'm sort of half-way there but need some help sorting out the pagination and getting it to work with the search query.

下面是我的HomeController:

Here is my HomeController:

public ActionResult Index(String query, int? page)
{
        // limit the number of articles per page
        const int pageSize = 4;
        // build the query
        var ArticleQuery = from a in _db.ArticleSet select a;
        // check if their is a query
        if (!string.IsNullOrEmpty(query))
        {
            ArticleQuery = ArticleQuery.Where(a => a.headline.Contains(query));
        }
        // orders the articles
        var OrderedArticles = ArticleQuery.OrderByDescending(a => a.posted);
        // takes the ordered articles and paginates them
        //var paginatedArticles = new PaginatedList(OrderedArticles.Skip((page ?? 0) * pageSize).Take(pageSize), page ?? 0, pageSize);
        var paginatedArticles = new PaginatedList<Article>(OrderedArticles, page ?? 0, pageSize);
        // return the paginated articles to the view
        return View(paginatedArticles);
}

的想法是,控制器显示4个项目每页会按日期排序。这里是查看我对指数的方法:

The idea is that the Controller shows 4 items per page will order them by date. Here is the View I have for the Index method:

<ul id="pagination">
    <% if (Model.PreviousPage) { %>
        <li><%= Html.ActionLink("<< First Page", "Index")%></li>
        <li><%= Html.ActionLink("<< Previous Page", "Index", new { page=(Model.PageIndex-1) }) %></li>
    <% } %>
    <% if (Model.NextPage) { %>
        <li><%= Html.ActionLink("Next Page >>", "Index", new { page = (Model.PageIndex + 1) })%></li>
        <li><%= Html.ActionLink("Last Page >>", "Index", new { page = (Model.TotalPages - 1) })%></li>
    <% } %>    
</ul>

我们的想法是,如果条件为真,这两个分页链接才会显示。

The idea is that these two pagination links will only show if the conditions are true.

最后这里是寻呼机的PaginatedList类:

Finally here is the PaginatedList class for the pager:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace NewsApp.Models
{
    public class PaginatedList<T> : List<T>
    {
        public int PageIndex { get; private set; }
        public int PageSize { get; private set; }
        public int TotalCount { get; private set; }
        public int TotalPages { get; private set; }

        public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = source.Count();
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);

            this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
        }
        public bool HasPreviousPage
        {
            get
            {
                return (PageIndex > 0);
            }
        }
        public bool HasNextPage
        {
            get
            {
                return (PageIndex + 1 < TotalPages);
            }
        }
    }

注意:我不想用任何第三方组件,如MVCContrib等,因为这是一个大学分配,以便将击败目的

分页现在工作正常,但是当我做了搜索,并有例如?/查询=测试中,我希望能够到页面的结果,此刻,他们迷路:/

The pagination works fine now but when I do a search and have e.g. /?query=test I want to be able to page the results, at the moment they get lost :/

感谢。

推荐答案

您没有提到你在做什么实际的错误是,但我想我看到什么是错的:

You didn't cite what you're actual error is, but I think I see what's wrong:

这两条线将返回包含您的一个IEnumerable的结果。

These two lines will return your an IEnumerable containing your results.

var paginatedArticles = OrderedArticles.Skip((page ?? 0) * pageSize).Take(pageSize).ToList();

return View(paginatedArticles);

然而,根据你的看法和您发布的辅助类,你认为期待 PaginatedList 对象作为它的模型。

如果是这样的话,我会做以下(更新

If that's the case, I'd do the following (updated):

var paginatedArticles = new PaginatedList( OrderedArticles, page ?? 0, pageSize );

return View(paginatedArticles);

和那么你认为应该有适当的模型被返回给它。

And then your view should have the proper model being returned to it.

这是说:我不知道你为什么不使用存储库层,但除非你的应用程序只有一个页面几乎总是有一个到位是个好主意。

That said: I don't know why you aren't using a repository layer, but unless your app is only a single page it is almost always a good idea to have one in place.

更新 --here就是我认为的完全控制逻辑应该是:

Update --here's what I think the full controller logic should be:

public ActionResult Index(String query, int? page)
{
    const int pageSize = 4;

    var ArticleQuery = from m in _db.ArticleSet select m;

    // Searching
    if (!string.IsNullOrEmpty(query))
    {
        ArticleQuery = ArticleQuery.Where(m => m.headline.Contains(query));
    }

    var OrderedArticles = ArticleQuery.OrderByDescending(m => m.posted);

    var paginatedArticles = new PaginatedList( OrderedArticles, page ?? 0, pageSize );
    // this will now be of type paginatedList. this class handles all of the paging for you, so no need to do that ahead of time

    return View(paginatedArticles);
}

在这种情况下,PaginatedList不是一个帮手--its全上需要创建并传递给你的模型类。在这个词的严格定义助手就是不同多少事,可能就不会在你的情况在这里工作。

In this case, PaginatedList is NOT a helper --its a full on class that needs to be created and passed to your model. A helper in the strict definition of the word is done much differently and probably wouldn't work in your case here.

这篇关于(作业)MVC分页帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆