InvalidOperationException: The 'Microsoft.AspNetCore.Mvc.ViewFeatures.Infrastructure.DefaultTempDataSerializer' cannot serialize an object of type 'System.Collections.Generic.List`1

Question: How do you solve: "InvalidOperationException: The 'Microsoft.AspNetCore.Mvc.ViewFeatures.Infrastructure.DefaultTempDataSerializer' cannot serialize an object of type 'System.Collections.Generic.List`1"

Answer: In order to work with TempData, you need to Serialize the Data object into a string first then store it into the TempData["MyObject"], when you need to retrieve the object from the TempData["MyObject"] you simply need to Deserialize the String into an object again.

See code below:

TempData["My"] = JsonSerializer.Serialize(MyObjectInstance);

MyObject my = JsonSerializer.Deserialize(TempData["My"].ToString())


Edit this Article
cannot serialize an object of type 'System.Collections.Generic.List`1 .NET 6 published
Was this page helpful?
Yes Yes! this solution to the problem was helpful. No No! this solution to the problem was not helpful.. Leave ErnesTech Feedback.Feedback
If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.

Your Session is Ending
Login to Continue

© 2021 - ErnesTech - Privacy