2

我正在为我的 mvc 项目构建一个分页并且有以下问题。我为分页做了一切,现在只需要传递一个页面信息来查看视图中的数据。

我有一个用户控制分页:

分页.ascx:

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Pagination.ascx.cs"
        Inherits="PIMP.Web.TestForum.Views.Shared.Pagination" %>

    <ul id="pagination-flickr">
        <% if (ViewData.Model.HasPreviousPage)
           { %>
        <li class="previous"><a href="<%=ViewData.PageActionLink.Replace("%7Bpage%7D", (ViewData.PageIndex - 1).ToString())%>">
            « Previous</a></li>
        <% }
           else
           { %>
        <li class="previous-off">« Previous</li>
        <% } %>
        <%for (int page = 1; page <= ViewData.Model.TotalPages; page++)
          {
              if (page == ViewData.Model.PageIndex)
              { %>
        <li class="active">
            <%=page.ToString()%></li>
        <% }
             else
             { %>
        <li><a href="<%=ViewData.PageActionLink.Replace("%7Bpage%7D", page.ToString())%>">
            <%=page.ToString()%></a></li>
        <% }
             }

          if (ViewData.Model.HasNextPage)
          { %>
        <li class="next"><a href="<%=ViewData.PageActionLink.Replace("%7Bpage%7D", (ViewData.PageIndex + 1).ToString())%>">
            Next »</a></li>
        <% }
            else
            { %>
        <li class="next-off">Next »</li>
        <% } %>


    </ul>
    <ul id="pagination-flickr0">


<li><a href="<%=ViewData.PageActionLink.Replace("%7Bpage%7D", page.ToString())%>"></a></li>

    </ul>

分页.ascx.cs:

public partial class PaginationViewData
    {
        public int PageIndex { get; set; }
        public int TotalPages { get; set; }
        public int PageSize { get; set; }
        public int TotalCount { get; set; }
        public string PageActionLink { get; set; }
        public bool HasPreviousPage
        {
            get
            {
                return (PageIndex > 1);
            }
        }

        public bool HasNextPage
        {
            get
            {
                return (PageIndex * PageSize) <= TotalCount;
            }
        }
    }

    public partial class Pagination : System.Web.Mvc.ViewUserControl<PaginationViewData>
    {
        public Pagination()
        {

        }
    }

类 PagedList.cs:

namespace PIMP.Web.TestForum.DataObject.Forum
{
    public class PagedList<T>: List<T>
    {
        public PagedList(IQueryable<T> source, int index, int pageSize)
        {
            this.TotalCount = source.Count();
            this.PageSize = pageSize;
            this.PageIndex = index;
            this.AddRange(source.Skip((index - 1) * pageSize).Take(pageSize).ToList());

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


        public PagedList()
        {

        }

        public int TotalPages
        {
            get;
            set;
        }

        public int TotalCount
        {
            get;
            set;
        }

        public int PageIndex
        {
            get;
            set;
        }

        public int PageSize
        {
            get;
            set;
        }

        public bool HasPreviousPage
        {
            get
            {
                return (PageIndex > 1);
            }
        }

        public bool HasNextPage
        {
            get
            {
                return (PageIndex * PageSize) <= TotalCount;
            }
        }
    }

    public static class Pagination
    {
        public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index, int pageSize)
        {
            return new PagedList<T>(source, index, pageSize);
        }

        public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index)
        {
            return new PagedList<T>(source, index, 10);
        }
    }
}

在视图中我正在执行以下操作:

<% Html.RenderPartial("Pagination", new NISE.Web.TestForum.Views.Shared.PaginationViewData()
      {
          PageIndex = ViewData.Model.PageIndex,
          TotalPages = ViewData.Model.TotalPages,
          PageActionLink = Url.Action("Forum", "Thread", new { id = this.Model.Id, page = "{page}" }),
          TotalCount = ViewData.Model.TotalCount,
          PageSize = ViewData.Model.PageSize
      }, null);%>

我不知道我应该在我的控制器中做什么。如何将页面信息传递给 ViewData?我的控制器看起来像这样,你能帮我扩展它吗?

 [HttpGet]
        [ValidateInput(false)]
        public ActionResult Thread(Guid id)
        {
            try
            {
                ThreadModel model = new ThreadModel(id);
                model.IncreaseHitCount();

                PagedList<ListView> list = new PagedList<ListView>();
                list.PageIndex = 0;
                list.PageSize = 0;
                list.TotalCount = 0;
                list.TotalPages = 0;

                return View(model);
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionHelper.Handle(ex, "Business Logic");
                if (rethrow)
                {
                    throw;
                }
            }

            return View();
        }
4

1 回答 1

1

好吧,我是这样做的:

我创建了一个(我的自定义)数据网格的“视图模型”,其中包含有关当前页面、页面大小等的信息。

它看起来像这样:

using System.Collections.Generic;

public class GridContent<T>
{
    public IEnumerable<T> Data { get; set; }
    public int PageIndex { get; set; }
    public int TotalPages { get; set; }
    public int TotalItems { get; set; }
}

然后在控制器中返回以下内容:

return View(new GridContent<Entity>()
            {
                Data = entityEnumeration, // your actual data
                PageIndex = pageIndex,
                TotalItems = totalItems,
                TotalPages = totalItems / pageSize + 1
            });

最后在视图上

<%@ Page Title="SomeTitle" Language="C#" Inherits="System.Web.Mvc.ViewPage<GridContent<Entity>>" %>

现在您可以使用包装模型/类 GridContent 访问分页信息和数据。

希望这可以帮助。

于 2010-10-06T10:12:42.183 回答