Click here to Skip to main content
14,422,439 members
Rate this:
Please Sign up or sign in to vote.
See more:
My objective is to fetch a large subset of data (or the entire set) from the database and then page it logically when rendering it on screen.

I need my controller to remember what was fetched for the duration of the cshtml page only. If I use session state or tempdata (instructed to remember the list), then I face the resultset being retained in memory after the user navigates away. So keep the result set in memory while the user is paging through it, then get rid of it when the user navigates away.

Is there a construct I can use, perhaps something like an old school destructor?

What I have tried:

I have a report in Asp.NET core 2.2 that currently visits the database for each page of data and then selects the desired page.
I can trim this to fetch just the desired page, but this does not solve the problem of a database call per page.
Posted
Updated 4 days ago
Comments
F-ES Sitecore 13-Jan-20 5:16am
   
What if the user never leaves the page? Let's say I navigate to your page then leave my computer running while I get something to eat, watch some telly and forget all about your page? Won't your data effectively be cached forever anyway?

If it was me I'd use some kind of sliding expiration on the cached data so it is invalidated if not requested for a few minutes.
MadMyche 13-Jan-20 6:51am
   
kinda like how a "session" works.. and what MS tried to "fix" with the WebForms "formadata" field...
Kornfeld Eliyahu Peter 14-Jan-20 3:00am
   
The data is to large to cache it at the client?
Ger Hayden 5 days ago
   
Yes, if you never leave the page, then it remains cached, I would be happy with that. But I want it gone as soon as the user goes to a new page, and while for now the dataset is small, this is for the eventuality that it becomes too large for caching.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

If you don't want a database call for each page of data, then the best option is to return all of the data to the client, and use Javascript to page the data.

For example, you could render all of the data as one large <table>, and then use DataTables[^] to turn it into a paged list.

Trying to store all of the data in the session for each user will be significantly less efficient than loading a single page at a time from the database.
   
Comments
Ger Hayden 4 days ago
   
Absolutely correct. Some combination of database calls and paging is going to have to happen. Either I allow entity framework to pull back pages as they are called, use the paged list (as I am currently doing with key bug that needs fixing) or page in javascript.

My preference is the paged list approach.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100