Hello, I'm trying to make a graph, while the data would be taken from the model (date and number of points), which was created on the principle of CodeFirst.

Here is a model

using System; namespace PersonalArea.DAL.Models { public class Result { public string Id { get; set; } public string PatientId { get; set; } public string GameName { get; set; } public string Time { get; set; } public int Score { get; set; } public int Level { get; set; } public DateTime FirstEnter { get; set; } public DateTime DateEnter { get; set; } public DateTime DateExit { get; set; } public string DifficultLevel { get; set; } public virtual Patient Patient { get; set; } } } 

Here is the controller

 [HttpGet] public async Task<IActionResult> Results(string id) { if (string.IsNullOrEmpty(id)) { return NotFound(); } Patient patient = await _userManager.FindByIdAsync(id) as Patient; if (patient == null) { return NotFound(); } List<IGrouping<string, Result>> results = _context.Results.Where(x => x.PatientId == id).GroupBy(z => z.GameName).ToList(); ViewBag.Test = JsonConvert.SerializeObject(results); return View(results); } 

Here is the view where I want to see the graphics.

 <script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script> <div id="chartContainer" style="height: 400px;"></div> <script type="text/javascript"> window.onload = function () { var test = @Html.Raw(ViewBag.test); var datapoints = []; for (var i = 0; i < test.length; i++) { datapoints.push({ y: test[i].Score, x: test[i].ID, label: test[i].GameName }); } window.onload = function () { var chart = new CanvasJS.Chart("chartContainer", { theme: "theme2", animationEnabled: true, title: { text: "Simple Column Chart in ASP.NET MVC" }, subtitles: [ { text: "Try Resizing the Browser" } ], data: [ { type: "column", //change type to bar, line, area, pie, etc dataPoints: datapoints /*[ { x: 10, y: 71 }, { x: 20, y: 55 }, { x: 30, y: 50 }, { x: 40, y: 65 }, { x: 50, y: 95 }, { x: 60, y: 68 }, { x: 70, y: 28 }, { x: 80, y: 34 }, { x: 90, y: 14 } ]*/ //Uncomment below line to add data coming from the controller. //dataPoints: @Html.Raw(ViewBag.DataPoints), } ] }); chart.render(); }; } 

But the system throws an error

An unhandled exception occurred while processing the request.

JsonSerializationException: Self referencing loop detected with type 'PersonalArea.DAL.Models.Result'. Path '[0] [0] .patient.results'.

And refers to the string in the controller

 ViewBag.Test = JsonConvert.SerializeObject(results); 

Please tell me what the problem is and how can this be solved?

  • Work with the model. Avoid ViewBag. You pass the received data to the return View(results) . Make a strongly typed view that will accept the @model List<Group<string, Result>> and work with the @Model model - WebMorda

1 answer 1

I understand that the fact is that my class cannot be exported to Json. Could it be because a class that is compiled in another class is a link. After all, each result is interplayed with the patient.