2

I am building a pagination my mvc project and have follew problem. I did everything for pagination and now need just pass a page information to view data in view.

I have a user control Pagination:

Pagination.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>

Pagination.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()
        {

        }
    }

class 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);
        }
    }
}

in View I am doing following:

<% 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);%>

I dont know what I should do in my controller. How can I pass Page Information to the ViewData? My controller looks like that, can you help me to extend it??

 [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();
        }
6
  • So you want to send "PagedList<ListView> list" to your view correct? But you also want to send the hit count, right? Commented Aug 27, 2010 at 15:05
  • yes, but i dont know how do that Commented Aug 27, 2010 at 15:11
  • Are you the one who wrote the ThreadModel class? Commented Aug 27, 2010 at 15:12
  • yes i have thread model, it is not posted here.. iam doing nothing with it for paging. do i need to set page values in thread model constructor instead controller? Commented Aug 27, 2010 at 15:22
  • That could help, however, because I don't know everything involved with it, I can't say if that's the BEST route. You can only pass one model to the view, so that's a bummer. Now, your tag for this question doesn't say MVC2 so I don't know if this works or not, but if in your controller you put "ViewMessage["PageSize"] = list.PageSize;" and the like, then in your view, wherever you put "ViewMessage["PageSize"]", the value that you set it to will be there. Again, I don't know if this is only in MVC2, but I thought I'd throw it out there. Commented Aug 27, 2010 at 16:01

1 Answer 1

1

Well, I did it like this:

I created a "view-model" of (my custom) data grid which contains infos about the current page, page size, etc.

it looks like this:

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; }
}

in the controller I then return the following:

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

and finally on the view

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

Now you can access the pagination information as well as the data using the wrapper model / class GridContent.

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.