Francis Cebu
Francis Cebu

Reputation: 617

Paging of record in ASP.NET with Session Variable

I'm creating a pagination in ASP.NET MVC 4 and the way i get my record from database is through webservice, after receiving the record from the webservice i save it in my session variable. but before i proceed on calling a web service, it will execute first a query in my session, if the query returns a value i will not proceed on calling on web service but if the query returns null, it will proceed on calling on webservice. here's my code.

[HttpPost]
public ActionResult Getitems(int take,int skip)
{
  var check = ((List<Records>)Session["tempRecord"])
               .Orderby(item => item.ID)
               .Skip(skip)
               .Take(take).ToList();

  if (check.count == 0)
  {
        //code for calling a webservice
        string Result = ServiceCaller.Invoke("Record/getRecord?skip=" + skip + "&take=" + take,"POST","Application/Json");
        List<Records> ListResult = JsonHelper.Deserialize<List<Records>>(Result);            

        //code for appending result in the session
        ((List<Records>)Session["tempRecord"]).addRange(ListResult);
        return Json(ListResult);
  }
  else
  {
        return Json(Check);
  }
}

Now, here's the problem. when the page load, the current value is the session is the initial value,but when the user click first a higher Page number, it will proceed on the code above, but since the check variable returns 0 count (because it's still have the initial value), it will call on the web service and add the session. then, when the user click on the page lower than the first page he/she clicks, it already returns the same value, since the session when being query already have a value.

Example you have total of 5 records (item1,item2,item3,item4,item5) in your table.

every page is 1 record, and the initial value of session is (item1) so here's what it looks like

| 1 | 2 | 3 | 4 | 5 |

when the user click the page "4" it will be look like this and the current value of session will be like this (item1,item4)

| 1 | 2 | 3 | 4 | 5 |

but when the user click on the page lower than page "4", the output is still the same "item4", but when the user click on the higher than page "4" it works fine.

any suggestion???

Upvotes: 0

Views: 1753

Answers (1)

Tristan
Tristan

Reputation: 352

So if I get it right, you've got something like a gridview. With paging? If so why dont you just send the page number? Why 2 variables?

Can't you send page number and then do something like this:

public ActionResult GetItems(int page){

var numberitems = 10;
var skip = page * numberitems;
var take = skip+numberitems;

    //code for calling a webservice
    string Result = ServiceCaller.Invoke("Record/getRecord?skip=" + skip + "&take=" + take,"POST","Application/Json");
    List<Records> ListResult = JsonHelper.Deserialize<List<Records>>(Result);            

    //code for appending result in the session
    ((List<Records>)Session["tempRecord"]).addRange(ListResult);
    return Json(ListResult);
}

Seems kind of weird getting take and skip from your View...

Edit:

public ActionResult GetItems(int page){
   var numberitems = 10;
   var skip = page * numberitems;
   var take = skip+numberitems;
if(Session["PageID"] == null || Session["cacheRecord"] == null){
   //code for calling a webservice
   string Result = ServiceCaller.Invoke("Record/getRecord?skip=" + skip + "&take=" + take,"POST","Application/Json");
   List<Records> ListResult = JsonHelper.Deserialize<List<Records>>(Result);            

   //code for appending result in the session
   ((List<Records>)Session["cacheRecord"]).addRange(ListResult);
   Session["PageID"] = page;
   return Json(ListResult);
 }else{
if(Session["PageID"] == page){
      var result = ((List<Records>)Session["tempRecord"])
           .Orderby(item => item.ID)
           .Skip(skip)
           .Take(take).ToList();
      return Json(result);
    }else{
   Session["PageID"] = page;
       string Result = ServiceCaller.Invoke("Record/getRecord?skip=" + skip + "&take=" + take,"POST","Application/Json");
       List<Records> ListResult = JsonHelper.Deserialize<List<Records>>(Result);            

       //code for appending result in the session
       ((List<Records>)Session["tempRecord"]).addRange(ListResult);
  return Json(ListResult);

}

}

Upvotes: 2

Related Questions