2 Replies - 252 Views - Last Post: 08 August 2017 - 10:42 AM

#1 andrewsw  Icon User is online

  • lashings of ginger beer
  • member icon

Reputation: 6339
  • View blog
  • Posts: 25,562
  • Joined: 12-December 12

retain filtered context

Posted 08 August 2017 - 12:16 AM

(ASP.NET Core MVC)

I am using comboboxes to filter records (details of the filtering were discussed in a recent topic here).

So now I have my filtered context, var filteredContext = _context.MyData.Where(...).

I could attach to the end of this .SingleOrDefaultAsync(); to get the id of the first record that meets the criteria, and then use my Edit(int? id) method to show and edit this record.

However, I need this page to have Next and Previous buttons to wander through the filtered records.

How can I retain the filtered context so that Next and Previous buttons would navigate the filtered list, not the full context, please?

Is This A Good Question/Topic? 0
  • +

Replies To: retain filtered context

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13395
  • View blog
  • Posts: 53,453
  • Joined: 12-June 08

Re: retain filtered context

Posted 08 August 2017 - 06:43 AM

From my code over here you can get pagination with 'skip' and 'take'.
            int startIndex = 1;
            int pageSize = 2;
            var nutz = foo.Skip(startIndex).Take(pageSize);


Ultimately you could just dump the data collection in one session variable, the 'start index' in another, and either have the page size as constant or pass that along too.
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw  Icon User is online

  • lashings of ginger beer
  • member icon

Reputation: 6339
  • View blog
  • Posts: 25,562
  • Joined: 12-December 12

Re: retain filtered context

Posted 08 August 2017 - 10:42 AM

Thanks again.

I did (eventually) use session data. I got a list of the filtered id's and put them into TempData as a joined string. TempData is a little awkward as, having read the data, I have to reset it again, but it works.

On load I find the current id, then store its next and previous values/id's (or the current id if there isn't a next or previous) and use these values with the Next/Previous buttons. [I'm not paginating, but moving between individual records.]



To explain for anyone interested:

To use TempData we have to initiate session data in Startup.cs.

  • In the controller, TempData["FilteredIds"] = "3,4,5,6,8,12";
  • The model-data for the View contains the current id
  • In the View, in Razor code, var filteredArray = TempData["FilteredIds"] (split)
  • Use the current id to find the next and previous id's in filteredArray: store these in C# variables
  • Use these variables to configure the Next and Previous buttons
  • Don't forget to reset TempData["FilteredIds"] = filteredArray (join with ,) because, as soon as it was read, it was cleared.

I'm not suggesting that this is the "best" way, but it fulfilled my current needs.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1