简体   繁体   English

使用ASP.NET MVC C#进行分页

[英]paging with asp.net mvc c#

Hello its my code where I giving out paging of view: 你好,我的代码在这里给出分页视图:

<div style="float: right;">
        <%= Html.RouteLink("<<", new { page = 1 }, new { title = "first page" })%>
        <% if (this.Model.HasPreviousPage)
           {%>

        <%= Html.RouteLink("<", new { page = (Model.PageIndex - 1) }, new { title = "previous page"})%>
        <%} %>

        <%  
            for (int i = 1; i <= this.Model.PageIndex + 2; i++)
            { if(i <= this.Model.TotalPages){
                 %>

        <%= Html.ActionLink(Convert.ToString(i), "Overview", new { page = i }, new { title = i + " page"})%>
        <% }}%>

        <% if (this.Model.HasNextPage)
           {%>

        <%= Html.RouteLink(">", new { page = (Model.PageIndex + 1) }, new { title = "next page"})%>
        <%} %>
        <%= Html.RouteLink(">>", new { page = Model.TotalPages }, new { title = "last page" })%>
    </div>

it looks like this: 它看起来像这样:

<< 1 2 3 > >>

I want to show always just a two pages before and after selected page. 我想始终只显示所选页面前后的两页。 If Iam on page 4, I will see all 3 pages before like this. 如果Iam在第4页上,我将在此之前看到所有3页。

<< < 1 2 3 **4** > >>

How can I do it, help me please.. ia little in stock in my mind at the moment, have no idea how to manipulate this. 我该怎么做,请帮助我。.目前我脑子里只有一点存货,不知道该如何操作。

my model has following data for paging: 我的模型具有以下用于分页的数据:

  /// <summary>
        /// Gets the boolean value of previous page
        /// </summary>
        public bool HasPreviousPage
        {
            get
            {
                return (PageIndex > 1);
            }
        }

        /// <summary>
        /// Gets the boolean value of next page
        /// </summary>
        public bool HasNextPage
        {
            get
            {
                return (PageIndex + 1 <= TotalPages);
            }
        }

        /// <summary>
        /// gets total pages of overview.aspx
        /// </summary>
        public int TotalPages
        {
            get
            {
                return _totalPages;
            }
            set
            {
                _totalPages = value;
            }
        }

        /// <summary>
        /// gets total count 
        /// </summary>
        public int TotalCount
        {
            get
            {
                return _totalCount;
            }
            set
            {
                _totalCount = value;
            }
        }

        /// <summary>
        /// gets actual page index
        /// </summary>
        public int PageIndex
        {
            get
            {
                return _pageIndex;
            }
            set
            {
                _pageIndex = value;
            }
        }

        /// <summary>
        /// gets page size
        /// </summary>
        public int PageSize
        {
            get
            {
                return _pageSize;
            }
            set
            {
                _pageSize = value;
            }
        }




 PageSize = 5;
                PageIndex = page;
                TotalCount = Threads.Count;
                TotalPages = TotalCount / PageSize;

                int pageResult = 0;
                for (int counter = 1; pageResult < this.TotalCount; counter++)
                {
                    pageResult = counter * this.PageSize;
                    TotalPages = counter;
                }

                FirstThreads = new List<Thread>();
                FirstThreads.AddRange(Threads.Skip<Thread>((PageIndex - 1) * PageSize).Take<Thread>(PageSize));

Well first of all, your View is fairly complicated. 首先,您的视图相当复杂。

Why not use a HTML Extension method to simplify both the UI and Paging? 为什么不使用HTML扩展方法来简化UI和分页?

There are numerous examples around, i personally like this one. 周围有很多例子,我个人很喜欢这个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM