views:

627

answers:

2

I have an action method that that returns a PagedList<> after a form's POST request.
I would like to add paging to this page, but all paging scenarios only seem to work with GET requests.

Currently the only way of adding paging controls is adding a bunch of forms with one button for navigating the page. This will look but ugly (all form buttons) and impose a lot of overhead because each of the forms will need a bunch of hidden fields (about 10) to transfer the needed parameters.

Is there a clean way to add about 12 optional parameters to a GET request? Or maybe there is an even better way?

A: 

The way to add many optional parameters to a GET request is to use query string parameters. Make the arguments to your controller action nullable, and the MVC framework will fill them with query string parameters if the user supplies them, and leave them null otherwise.

I do not believe that using POST for paging is, generally, appropriate. POST is intended to update a server resource. Paging doesn't do that.

Craig Stuntz
Paging is a navigation element. If the web page happens to contain both a form and a paged list of some kind, which the application I'm currently working on does, then paging will need to POST and update the server.
Todd Smith
My app does this, too, but: Those are two separate concerns. (1) The POST updates the server. (2) You go somewhere after the post. It's advantageous to do this with a GET. See: http://blog.eworldui.net/post/2008/05/ASPNET-MVC---Using-Post2c-Redirect2c-Get-Pattern.aspx for why.
Craig Stuntz
You don't always "go somewhere after the post" if its an AJAX update.
Todd Smith
But the separation of concerns is still the same.
Craig Stuntz
I can't take this "POST is for update" crap anymore. Have you ever tried to transfer senstive data (e.g. from a search form) using GET? If you are doing REST, yes, POST is for update, but if you are working with an actual browser and not some custom program or AJAX, there's only GET and POST.
Jabe
You are (1) ignoring the actual question, (2) contradicting yourself, and (3) missing that my answer was an explicit generalization, not a hard and fast rule, all in one comment!
Craig Stuntz
+1  A: 

Each paging button has one unique value you need to submit along with the rest of your form values. So you really only need something such as:

function GoToPage(pageNumber)
{
    // submit form along with pageNumber

    return false;
}

<a href="javascript:GoToPage(1);">1</a>
<a href="javascript:GoToPage(2);">2</a>
<a href="javascript:GoToPage(3);">3</a>
Todd Smith