Self referencing loop detected with type 'Forms.Data.Model'.

"Self-referencing loop detected," occurs when a JSON serializer encounters a loop in object references that it cannot resolve, leading to an infinite loop during serialization or deserialization

The error you're encountering, "Self-referencing loop detected," occurs when a JSON serializer encounters a loop in object references that it cannot resolve, leading to an infinite loop during serialization or deserialization. This issue is common when objects reference each other in a circular manner.


 

Solution

Step 1: Configure JsonSerializerSettings to Handle Reference Loops You can configure the JsonSerializerSettings to handle self-referencing loops by ignoring them. This is achieved with the ReferenceLoopHandling setting:

TempData["FormAndQuestionsById"] = JsonConvert.SerializeObject(data, Formatting.Indented, 
    new JsonSerializerSettings
    {
        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
    });

Step 2: Retrieving and Deserializing Data from TempData

var json = TempData["FormAndQuestionsById"] as string;
if (json != null)
{
    // Deserialize JSON to the original data type
    var formData = JsonConvert.DeserializeObject<FormDto>(json, new JsonSerializerSettings
    {
        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
    });
}

Experienced Software Developer with a demonstrated history of working in the Information Technology and services industry. Strong engineering professional skilled in DotVVM, AutoDesk Forge API's, C#, ASP.NET MVC, SQL Server, Angular,React JS, ASP.NET Core, Web API, Javascript, Jquery, Windows forms, WPF, ASP.NET WebForms, Nopcommerce, Python, Azure Open AI, Azure DevOps.

To start an initial chat, just drop me an email at prince@debugtutorial.com or use the form on the contact page.