Hi. I have some queries that have some outputs in their select part. Now I want to send those queries outputs from controller into view but there is an error when sending them like the following:
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List`1[Dashboard.Models.ChartModel]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.List`1[Dashboard.TempClasses.ChartListObjects]'.
for instance one of my query name is avg and to get its data, In controller there is a part of my code like the following:
part of my controller code
{
namespace Dashboard.Controllers
{
public class HomeController : Controller
{
private readonly CSSDDashboardContext _context;
public HomeController(DashboardContext context)
{
_context = context;
}
public IActionResult Index()
{
var lstModel = new List<ChartModel>();
var vm = new List<ChartListObjects>();
foreach (var index in avg)
{
lstModel.Add(new ChartModel
{
DimensionOne = index.Month,
Quantity = (int)index.Avg
});
}
vm.GrantedAvg = lstModel;
foreach (var index in grantedAccessPercent)
{
lstModel1.Add(new ChartModel
{
DimensionOne = MonthName(index.Month),
Quantity = (int)index.pct
});
}
vm.GrantedPercent = lstModel1;
return view(vm);
}
}
Here is my ChartModel class
namespace Dashboard.Models
{
public class ChartModel
{
public int Quantity { get; set; }
public string DimensionOne { get; set; }
}
}
Here is my ChartListObjects class:
namespace Dashboard.TempClasses
{
public class ChartListObjects
{
public List<ChartModel> GrantedPercent { get; set; }
public List<ChartModel> AvgPercent { get; set; }
}
}
I can't understand what is the problem, I appreciate of any help. Thanks.
What I have tried:
namespace Dashboard.Models
{
public class ChartModel
{
public int Quantity { get; set; }
public string DimensionOne { get; set; }
}
}
And here is my part of view:
@using System.Linq;
@model List<Dashboard.TempClasses.ChartListObjects>
@{
var XLabels = Newtonsoft.Json.JsonConvert.SerializeObject(Model.Select(x => x.GrantedPercent.FirstOrDefault().DimensionOne).ToList());
var YValues = Newtonsoft.Json.JsonConvert.SerializeObject(Model.Select(x => x.GrantedPercent.FirstOrDefault().Quantity).ToList());
ViewData["Title"] = "Pie Chart";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Pie</title>
</head>
<body>
<div class="box-body">
<div class="chart-container">
<canvas id="chart" style="width:100%; height:500px"></canvas>
</div>
</div>
</body>
</html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(function () {
var chartName = "chart";
var ctx = document.getElementById(chartName).getContext('2d');
var data = {
labels: @Html.Raw(XLabels),
datasets: [{
label: "Drinks Chart",
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)',
'rgba(255, 0, 0)',
'rgba(0, 255, 0)',
'rgba(0, 0, 255)',
'rgba(192, 192, 192)',
'rgba(255, 255, 0)',
'rgba(255, 0, 255)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)',
'rgba(255, 0, 0)',
'rgba(0, 255, 0)',
'rgba(0, 0, 255)',
'rgba(192, 192, 192)',
'rgba(255, 255, 0)',
'rgba(255, 0, 255)'
],
borderWidth: 1,
data: @Html.Raw(YValues)
}]
};
var options = {
maintainAspectRatio: false,
scales: {
yAxes: [{
ticks: {
min: 0,
beginAtZero: true
},
gridLines: {
display: true,
color: "rgba(255,99,164,0.2)"
}
}],
xAxes: [{
ticks: {
min: 0,
beginAtZero: true
},
gridLines: {
display: false
}
}]
}
};
var myChart = new Chart(ctx, {
options: options,
data: data,
type:'pie'
});
});
</script>