❔ ❔ MVC - Chart - Model - NullReferenceException

I'm using google chart to display my data. The chart works by default, but when I change the foreach data dispay, it disappears. @model List<BloodSugarData> ...
function drawStuff() {

var button = document.getElementById('change-chart');
var chartDiv = document.getElementById('chart_div');

var data = google.visualization.arrayToDataTable([

@Html.Raw($"['User Phone', 'Date', 'Time of Day', 'Blood Sugar (mg/dl)'],")
@foreach (var item in Model)
{
@Html.Raw($"['{@item.userPhone}', {@item.measureDate}, {@item.timeOfDay}, {@item.mgBloodSugar}],")
}
]);
function drawStuff() {

var button = document.getElementById('change-chart');
var chartDiv = document.getElementById('chart_div');

var data = google.visualization.arrayToDataTable([

@Html.Raw($"['User Phone', 'Date', 'Time of Day', 'Blood Sugar (mg/dl)'],")
@foreach (var item in Model)
{
@Html.Raw($"['{@item.userPhone}', {@item.measureDate}, {@item.timeOfDay}, {@item.mgBloodSugar}],")
}
]);
20 Replies
cyb3rdragon
cyb3rdragonOP2y ago
The problem should be in the foreach line imo, but I don't know what +picture: the data shows up when I only use the same foreach in a table, but I need it in a chart
cyb3rdragon
cyb3rdragonOP2y ago
Update: System.NullReferenceException - Model
Angius
Angius2y ago
Show your backend code
cyb3rdragon
cyb3rdragonOP2y ago
Data class (Model)
cyb3rdragon
cyb3rdragonOP2y ago
BSDiagram Razor View
cyb3rdragon
cyb3rdragonOP2y ago
I can send code format too if needed
Angius
Angius2y ago
And the controller?
cyb3rdragon
cyb3rdragonOP2y ago
public ActionResult BSDiagram()
{
return View();
}
public ActionResult BSDiagram()
{
return View();
}
Angius
Angius2y ago
Well
cyb3rdragon
cyb3rdragonOP2y ago
I have to add the data into the controller< ?
Angius
Angius2y ago
You're not sending any data to the view So hard to expect it to be there
cyb3rdragon
cyb3rdragonOP2y ago
okay understand
Angius
Angius2y ago
Whatever you pass to View() is available as Model in the view The @model Foo in the view just tells it that the type of that Model will be Foo
cyb3rdragon
cyb3rdragonOP2y ago
cool! thank you, I just got confused haha
cyb3rdragon
cyb3rdragonOP2y ago
🙂
Angius
Angius2y ago
Nice
cyb3rdragon
cyb3rdragonOP2y ago
@model List<BloodSugarData>


<table>
<tr>
<th> Id </th>
<th> Phone </th>
<th> Date </th>
<th> Time of day </th>
<th> Blood sugar (mg/dl)</th>
</tr>

@{
for (int i = 0; i < Model.Count; i++)
{
var item = Model[i];

<tr>
<td>@item.Id </td>
<td>@item.userPhone </td>
<td>@item.measureDate </td>
<td>@item.timeOfDay </td>
<td>@item.mgBloodSugar </td>

</tr>
</table>
}
}
@model List<BloodSugarData>


<table>
<tr>
<th> Id </th>
<th> Phone </th>
<th> Date </th>
<th> Time of day </th>
<th> Blood sugar (mg/dl)</th>
</tr>

@{
for (int i = 0; i < Model.Count; i++)
{
var item = Model[i];

<tr>
<td>@item.Id </td>
<td>@item.userPhone </td>
<td>@item.measureDate </td>
<td>@item.timeOfDay </td>
<td>@item.mgBloodSugar </td>

</tr>
</table>
}
}
public IActionResult BSMeasurePageList()
{
return View(bsDatas);
}
public IActionResult BSMeasurePageList()
{
return View(bsDatas);
}
public static List<BloodSugarData> bsDatas = new List<BloodSugarData>
{

new BloodSugarData(0, "36703245432", date1, "morning", 4),
new BloodSugarData(1, "36405555555", date2, "night", 12),

};
public static List<BloodSugarData> bsDatas = new List<BloodSugarData>
{

new BloodSugarData(0, "36703245432", date1, "morning", 4),
new BloodSugarData(1, "36405555555", date2, "night", 12),

};
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
cyb3rdragon
cyb3rdragonOP2y ago
/close
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server