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".


Login to See the Rest of the Answer

Answer: That is right, the Lists in C-Sharp objects therefore you should normalize or convert the List into the String and then Serialize the String. Then deserialize the serialized String (in this case the List) from the View or Controller. Keep in mind that TempData["MyKey"] = MyObject works the same too. You can't just throw an Object into a TempData, you need to do extra work converting that into a #Serializable Object like String or J#SON Object.

Let me know in the comments below if this makes sense. 


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

Edited Version 2

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 and then store it in 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 the code below


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

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




For peering opportunity Autonomouse System Number: AS401345 Custom Software Development at ErnesTech Email AddressContact: [email protected]