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"

Please Login to see the rest of the answer

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())

Write an Article
If you log in, you will be notified when someone leaves a comment.

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

© 2022 - ErnesTech - Privacy