Using MVC3 Webgrid, how do I preserve the grid in the session so I can return to the page I was on before?

StackOverflow https://stackoverflow.com/questions/22459554

Pergunta

Sorry if the title is a little confusing, but basically my questions is this.

I am on Page A, Page A contains a search which filters data in a Webgrid. The webgrid has ten rows per page. I've searched for something in the filter, and the webgrid has now returned 27 rows of data, as there are 27 rows, there are now three pages displayed on my webgrid.

Now comes the bit I need help on, at the moment, I click a link in the webgrid and I'm moved away from the page onto Page B, at this point the page model is saved in the Session, so when I navigate back to Page A using a link, the webgrid still displays the same 27 rows I had when I initially searched. However, if I was on page 3 of the grid before on Page A, when I am returned to Page A from Page B, I am automatically back onto page 1.

How do I save the page number on the webgrid and enter it as an argument so that when I return to Page A from Page B, it takes me to page 3, not page 1 again.

Thanks in advance,

Foi útil?

Solução

Yes, as long as the webgrid posts back to a controller every time it pages, you could capture the current page number in session, and then when you come back, you can reload to where you need to go. Add this to your model:

public int PageNumber { get; set; }

In your controller, set the PageNumber to 1 for default, or the value stored in session when saved. This value gets passed to the view, which you can then pass to the webgrid, which takes a page number to show.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top