In my index I added a search field.
When user enter a search term and click filter the index (Index) is filtered. So far so good.
What I would like to achieve is that if the user performs other operations (edit, details, delete, etc) in the same controller and returns to the Index, I would like the search to be restored.
To do this, I used TempData
but without success.
In the various forums / tutorials I found conflicting about lifetime. Some say:
lifetime of an object placed in TempData is exactly one additional request.
See this stackoverflow article
On another site i found:
Data stored in TempData will be alive in the cookie until you read it from the TempData dictionary.
See this article
So where is the truth: Only one sub request or when I read the TempData ?
My tests says the second: "until you read it" (or when expire)
Here my code on startup
public void ConfigureServices(IServiceCollection services)
{
// [..]
// Relevant code only
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
// Set a short timeout for easy testing.
options.IdleTimeout = TimeSpan.FromMinutes(15);
options.CookieHttpOnly = true;
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// [..]
// Relevant code only
app.UseSession();
}
On my controller
public async Task<IActionResult> Index(int page, string search)
{
search = search ?? TempData["Search"]?.ToString();
// Query with search data
TempData["search"] = search;
}
If I search on this controller the TempData save the search term.
If, after searching in the list, I navigate to other pages and then return here, the search term is still present
I already know that exist .Keep
, .Peek
and other methods for manage the TempData
Questions
- How manage the search term between actions ?
- How work the TempData (until re-read or on ONE addictional request) ?
Session
and keep in mind its cookie-based essence learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state – Esquimau