Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

asp.net core mvc - Persist list of key/value pairs between the controller and views

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-5.0


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...