开发者

Paging with ASP.NET MVC (URL manipulation?)

Hi,

I am building a HTML helper in ASP.NET MVC to handle the paging of a forum/threads. A object will be passed from the modelView to the HTML helper that holds the followin information :

CurrentPageNr ObjectsPerPage TotalAmountOfObjects

My thought is to generate a html output that will render somthing like this :

[FirstPage] [PrivousePage] [1] [2] [3] [4] [5] [6] [7] [8]开发者_JAVA百科 [9] [NextPage] [LastPage]

The question is how to handle the click. The solutions I have found so far is the following :

Solution 1: Let each link be a button that sets a hidden field(currentPage) and then submits the form

Solution 2: I could manipulate the current url(absolutURI) and set it as links in the page. The problem here will be that the href could be vary long(for each link). The other problem is that I havent found a easy way to manipulate the querystring so if I have to do this manually it would be som work :

Replace : "&ASP_P=[currentPage]" Or Replace : "ASP_P=[currentPage]" if there is a ? before the ASP_P

Pleas Advice


What I would do is make another variable NumberOfPages which is (Total Size / ObjectsPerPage) rounded up.

Then simply loop through each "page" and generate the href for the links:

A crude example follows:

public static string GetPageNumberLink(this HtmlHelper helper, string nameOfPageParameter, int pageNumber)
{
       //set the placeholder
       string queryValues = helper.ViewContext.HttpContext.Request.Url.Query;
       helper.ViewContext.RouteData.Values[nameOfPageParameter] = "..PAGEHERE..";
       string virtualPath = helper.ViewContext.RouteData.Route.GetVirtualPath(helper.ViewContext.RequestContext, helper.ViewContext.RouteData.Values).VirtualPath;

       return helper.ResolveUrl("~/" + virtualPath.Replace("..PAGEHERE..", pageNumber.ToString())) + queryValues;
}


You can try this.

  public Index(int? page)
        {

int pageSize=10;    
var List= Repo.GetAllMembers();    
var paginatedList = List.Skip((page ?? 0) * pageSize).Take(pageSize).ToList();
return View(paginatedList);    
}

give pageSize as number of records you want to dispaly

Then Register a new mapping Rule using MapRoute() helper method in Global.asax


Rob Conery wrote an HTML Helper class for this for MVC. You can find the details here . I'm actually using it successfully on my site if you want to see it in action. Just scroll to the bottom of the page.


SnowJim,

I created a little example of this using thr IpgaedList interface as per rob connerys. I actually pointed another SO user at it a while baclk. You can find the replay here:

mvc - ajax pager - make current page inactive link

along with links to the download for the full example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜