ASP.NET MVC2 中可重用的可分页列表

发布于 2024-09-08 18:57:36 字数 273 浏览 4 评论 0原文

例如,此列表包含文档概述并需要允许分页。 该列表在整个网站中使用。

根据使用的上下文,它需要来自不同来源的数据。例如,它可以用在“组”页面上,需要加载组的文档。它可以在“事件”页面上使用,需要加载事件的文档。

这两种情况也可以对页面内的文档进行不同的过滤。

如果列表没有不同的数据源,我可以轻松使用 Html.RenderAction,并从那里开始工作。

但是我是否向调用者中的文档提供列表,或者列表是否应该加载文档,具体取决于过滤器/分页/... viewdata?

For example, this list contains an overview of documents and needs to allow paging.
The list is used throughout the website.

Depending on the context where it is used, it needs the data from a different source. For example, it can be used on a 'group' page, where it needs to load the documents for the group. And it can be used on an 'event' page, where it needs to load the documents for the event.

Both situations can also have different filtering on the documents withing the page.

Should the list not have different datasources, I could easily use Html.RenderAction, and start working from there.

But do I supply the list with documents in the caller, or should the list load the documents, depending on filter/paging/... viewdata?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

杀手六號 2024-09-15 18:57:36

您可以将列表实现为部分视图而不是控制器操作。然后,您可以使用 RenderPartial 渲染列表并根据列表应显示的内容传入不同的对象列表。

例如,如果您传入 IEnumerable 作为模型,您可以使用 Model.Skip(page * pagesize).Take(pagesize)

updated

之类的方法来实现分页。我们不要在视图中进行分页。创建一个模型类来执行分页并且实际上是可测试的,并将其传递到视图中以提供正确的文档页面可能是一个更好的主意。当然,视图仍然负责显示页面和链接到其他页面。

您可以创建类似 DocumentPager 类的内容,该类包装 IEnumerable 并执行分页操作。这看起来像这样,

public class DocumentPager {
    public IEnumerable<MyDocumentObject> DocSource { get; private set; }
    public int PageSize { get; private set; }

    public DocumentPager(IEnumerable<MyDocumentObject> docSource, int pageSize) {
        DocSource = docSource;
        PageSize = pageSize;
    }

    public IEnumerable<MyDocumentObject> GetPage(int pageNumber) {
        return DocSource.Skip(..etc).Take(..etc);
    }

    public int NumPages {
        get { return DocSource.Count() / PageSize; }
    }
}

然后您可以将其传递到视图和可以调用 GetPage 和 NumPages 方法和属性的部分视图中。

You could implement the list as a partial view instead of a controller action. Then you can use RenderPartial to render the list and pass in a different list of objects depending on what the list should display.

If you pass in an IEnumerable as a model for example you can implement paging using something like Model.Skip(page * pagesize).Take(pagesize)

updated

Let's not do paging in the view. It might be a better idea to create a model class that does the paging and is actually testable and pass that into the view to serve up the right page of documents. Of course the view is still responsible for showing pages and linking to other pages.

You could create something like a DocumentPager class that wraps an IEnumerable and that does paging. This would look something like this

public class DocumentPager {
    public IEnumerable<MyDocumentObject> DocSource { get; private set; }
    public int PageSize { get; private set; }

    public DocumentPager(IEnumerable<MyDocumentObject> docSource, int pageSize) {
        DocSource = docSource;
        PageSize = pageSize;
    }

    public IEnumerable<MyDocumentObject> GetPage(int pageNumber) {
        return DocSource.Skip(..etc).Take(..etc);
    }

    public int NumPages {
        get { return DocSource.Count() / PageSize; }
    }
}

You can then pass this into the view and into your partial view that can call the GetPage and NumPages method and property.

南冥有猫 2024-09-15 18:57:36

也许你可以用 ScottGu 的 PagedList 类做一些事情,可以在这里找到: http://blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/

Maybe you can do something with ScottGu's PagedList class which can be found here: http://blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文