【发布时间】:2018-03-13 05:21:55
【问题描述】:
我想要一个基于类 Result 的图表(我使用 CodeFirst)
我有课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; }
}
}
控制器中的方法
[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);
}
我的视图模型
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; }
}
}
还有我的 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 });
}
}
我有一个错误
JsonSerializationException:检测到类型的自引用循环 'PersonalArea.DAL.Models.Result'。路径“[0][0].patient.results”。
开启
ViewBag.Test = JsonConvert.SerializeObject(results);
请帮我解决这个错误
【问题讨论】:
标签: c# json asp.net-mvc serialization asp.net-core-mvc