Persist list of key/value pairs between the controller and views

413 Views Asked by At

I am working with C# in a .NET Core MVC web application and I would like to keep a persistent list of key/value pairs between the controller and views throughout multiple calls back to the controller accumulating the pairs as I go along. Is there a reasonably simple way to do this? I have tried looking into using the TempData[] technique, but this seems to become excessively complex when working with a list or collection of paired data.

I'd appreciate some ideas.

Thanks.

1

There are 1 best solutions below

1
Fei Han On

I would like to keep a persistent list of key/value pairs between the controller and views throughout multiple calls back to the controller accumulating the pairs as I go along.

I have tried looking into using the TempData[] technique, but this seems to become excessively complex when working with a list or collection of paired data.

Normally we store simple values (such as strings, numeric etc) in TempData. To store complex types using TempData, we could serialize it to a string-based format first.

If you do not want to do additional serialization with using TempData, to persistent list of key/value pairs data across requests, you can try to store these data in session.

For more information about state management in ASP.NET Core, you can check this official doc:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-5.0