This is a migrated thread and some comments may be shown as answers.

[Solved] Save grid state in Session, to persist between pages

3 Answers 182 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Shuchi
Top achievements
Rank 1
Shuchi asked on 31 Jan 2012, 09:58 PM
Hi
I have a class that encapsulates some filter data and an object to store GridState.

public class UserGridFilterModel
    {
        public string Title { get; set; }
        public object GridState { get; set; }
    }

I have the Index() and _Index() methods for grid's data binding.  
When the user sorts a grid column, the _Index() action is called and I persist the grid state in Session as follows:

[GridAction]
public ViewResult _Index()
        {                  
            UserGridFilterModel filterModel = new UserGridFilterModel();
                    
            filterModel.GridState = this.GridRouteValues();
 
            Session["UserGridFilter"] = filterModel;


Now when the user leaves the grid page, the most recent grid state is in Session. I would like to know how I can apply the saved grid state in the Index() method when the user comes back to it. I tried the following but it did not work:

public ViewResult Index()
      {
           
          UserGridFilterModel filterModel = new UserGridFilterModel();
           
          if (Session["UserGridFilter"] != null)
          {
              filterModel = (UserGridFilterModel)Session["UserGridFilter"];
          }
 
          if (filterModel.GridState != null)
          {
              ViewBag.GridState = filterModel.GridState;
          }

3 Answers, 1 is accepted

Sort by
0
Shuchi
Top achievements
Rank 1
answered on 31 Jan 2012, 11:49 PM
I have already looked at this demo : http://www.telerik.com/community/code-library/aspnet-mvc/grid/preserve-grid-state-between-pages.aspx 

I don't want to implement this approach as it requires code to be added in every action and action link:

public ActionResult Index()
        {
            ViewBag.GridState = this.GridRouteValues();
 
            return View(Data());
        }
 
Html.ActionLink("Details", "Details", (RouteValueDictionary)ViewBag.GridState)

I would like to save and retrieve the grid state in the session.
0
Shuchi
Top achievements
Rank 1
answered on 02 Feb 2012, 07:46 PM
Figured out the solution after looking at the following post:
http://www.telerik.com/community/forums/aspnet-mvc/grid/ability-to-set-the-filter-sort-with-a-string-the-exact-one-that-is-passed-back-in-querystring.aspx

In my Index() action, I check for gridstate in session. If it exists, I create a new routevaluedictionary and add the session data to it. I then redirect to index with this routevaluedictionary. In my method I only need the orderBy filter, as we do not have paging enabled. But this approach can be used to include all available gridstate key-value pairs.

filterModel = (UserGridFilterModel)Session["UserGridFilter"];
 
 if (filterModel.GridState != null)
            {
                RouteValueDictionary rvdFilter = (RouteValueDictionary)filterModel.GridState;
 
                if (rvdFilter["orderBy"] != null)
                {
                    RouteValueDictionary rvd = new RouteValueDictionary();
                    rvd.Add("GridID-orderBy", rvdFilter["orderBy"].ToString()); // format : [gridId-filterType] = filterValue(ex Title-desc)
 
                    return RedirectToAction("Index", rvd);
                }
            }
0
jason
Top achievements
Rank 1
answered on 15 Mar 2012, 09:48 PM
If I RedirectToAction Index from Index I get stuck in an inifinte loop.

This does not seems right
Tags
Grid
Asked by
Shuchi
Top achievements
Rank 1
Answers by
Shuchi
Top achievements
Rank 1
jason
Top achievements
Rank 1
Share this question
or