-2

I want a make some chart, based on class Result (i use CodeFirst)

I have class Results.cs

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; }
    }
}

And method in 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);
        } 

My ViewModel

using PersonalArea.DAL.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PersonalArea.WebUI.ViewModels
{
    public class ResultChartModel
    {
        public string Surname { get; set; }

        public IQueryable<IGrouping<string, Result>> Results { get; set; }
    }
}

And my View.cshtml

window.onload = function ()
        {
            var test = @Html.Raw(JsonConvert.DeserializeObject(ViewBag.test));
            var datapoints = [];

            for (var i = 0; i < test.length; i++)
            {
                var str_array = test[i].Score;

                for (var j = 0; j < str_array.length; j++)
                {
                    datapoints.push({ y: str_array[j], x: str_array[j], label: test[i].GameName });
                }
            }

And i have a error

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

On

ViewBag.Test = JsonConvert.SerializeObject(results);

Please, help my resolve this error

Travis Bincle
  • 61
  • 1
  • 9

1 Answers1

0

In your Model change like:

   public class ResultChartModel
    {
        public string Surname { get; set; }

        public List<Result> Results { get; set; }
    }

In your controller :

replace

    List<IGrouping<string, Result>> results = _context.Results.Where(x => x.PatientId == id).GroupBy(z => z.GameName).ToList();
    ViewBag.Test = JsonConvert.SerializeObject(results);

by

    List<Result> results = _context.Results.Where(x => x.PatientId == id).OrderBy(z => z.GameName).ThenBy(z => z.DateExit).ToList();
  ViewBag.Test = JsonConvert.SerializeObject(results);

Then Your View :

Change

var test = @Html.Raw(ViewBag.test);

To

var test = @Html.Raw(JsonConvert.DeserializeObject(ViewBag.Test));

JS:

    for (var i = 0; i < test.length; i++) {  

        datapoints.push({ y: test[i].Score x: test[i].DateExit, label: test[i].GameName });

 }

Hopefully it's help you.

Ashiquzzaman
  • 5,129
  • 3
  • 27
  • 38