0

I am new to asp and have written a project that will connect to a database, retrieve number records and then present those records to the user in a paginated table. The user can then click on a record to edit, make the edit, save the change and be returned to the original table view. The update page is strongly typed.

I am struggling to keep track of which pagination page was last viewed and then navigating back to it. I.e. if the user is on page 5 of 10, they then update a record in from page 5, when the edit is saved the table is shown again but it has gone back to page 1. What is the best method to keep track of the last pagination page?

Any help appreciated.

Chris

Chris
  • 318
  • 2
  • 5
  • 17

3 Answers3

0

Typically I will pass the page number in form submissions, and then when redirecting after save, pass it as part of the query string, so your action can provide the correct page of data back to the view.

This also lets users bookmark specific pages, which can be useful, or even just refresh the current page if necessary, without losing their place.

You may also need to pass sort information, if that feature is available.

D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
  • I did try this. I could pass the current pagination page to the edit page with no problem. I could not pass that value back to the controller when the edit was saved though. the edit page is stongly typed and the save is just a Submit struggled to add an additional parameter to this call: @using (Html.BeginForm("IndexEdit", "CgRefCodes", FormMethod.Post, new { id = "target"})) – Chris Nov 09 '12 at 15:21
0

I've found it's nice to save it in viewstate: ex:

ViewState("paging") = 1

Edwin
  • 1,458
  • 10
  • 17
0

In the end I used a session cookie on the server to keep track of the pages shown and last search. This meant I could navigate between pages/controller and keep track of which page was showing in each and didn't need to pass lots of parameters around. I added a new class:

public class SearchModel 
{
    public string TargetController { get; set; }
    public string TargetMethod { get; set; }
    public string OriginalSearchCriteria { get; set; }
    public string NewSearchCriteria { get; set; }
    public int Page { get; set; }

    public void SetCriteria(String newCriteria, int pageIn)
    {

        if (!newCriteria.Equals(OriginalSearchCriteria))
        {
            OriginalSearchCriteria = newCriteria;
            Page = 1;
        }
        else
        {
            Page = pageIn;
        }
    }

    public void SetPage(int newPage)
    {
        if (newPage != 0)
            Page = newPage;

    }


}

In the controller I just added:

private SearchModel GetSearch()
    {
       SearchModel search = (SearchModel)Session["CgRefCodeSearch"];
       if (search == null)
       {
          search = new SearchModel();
          search.Page = 1;
          search.OriginalSearchCriteria = "";
          Session["CgRefCodeSearch"] = search;
        }
        return search;
     }

On each function in the controller I could then reference this:

GetSearch().SetPage(page);
CurrentPage = GetSearch().Page etc...

This was based on stuff I read in this Pro ASP.NET MVC 3 Framework, Third Edition by Adam Freeman; Steven Sanderson. Its really simple but works OK.

Chris
  • 318
  • 2
  • 5
  • 17