❔ ❔ 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
cyb3rdragon2y 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
cyb3rdragon2y ago
Update: System.NullReferenceException - Model
Angius
Angius2y ago
Show your backend code
cyb3rdragon
cyb3rdragon2y ago
Data class (Model)
cyb3rdragon
cyb3rdragon2y ago
BSDiagram Razor View
cyb3rdragon
cyb3rdragon2y ago
I can send code format too if needed
Angius
Angius2y ago
And the controller?
cyb3rdragon
cyb3rdragon2y ago
public ActionResult BSDiagram()
{
return View();
}
public ActionResult BSDiagram()
{
return View();
}
Angius
Angius2y ago
Well
cyb3rdragon
cyb3rdragon2y 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
cyb3rdragon2y 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
cyb3rdragon2y ago
cool! thank you, I just got confused haha
cyb3rdragon
cyb3rdragon2y ago
🙂
Angius
Angius2y ago
Nice
cyb3rdragon
cyb3rdragon2y 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
cyb3rdragon2y 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
More Posts
❔ Get id from query or DTOImagine I have the next two entities class Task{ public int Id {get;set;} public string Desc {get;s✅ Is it possible to check how long it would take for the battery to finish charging?Hi, I know i got a lot of information form my previous thread, but I would still like to access more❔ ✅ How do I import System.Windows.Devices.Power?I know I need to make a namespace, but how? It doesn't show up on the list and I just want to aggrav❔ Capture parameter value with Moq decorated with the 'in' keywordHello! I'm writing Unit Tests for a library with a lot of structs, which utilizes the `in` keyword t✅ Mention user in footer in Embed (.Net 6)Hi, I'm trying to mention the user to used the command in the footer but I can only make it write itMap complex object (dictionary) from appsettings.json to modelHi, i have a complex appsettings.json like this ``` "interceptor": { "interactionType": "redire❔ Passing a list of values to Stored Procedure in EF CoreHey devs, I would like to know whether it is possible to pass a list of values as a parameter to a ✅ Input into list (Console app)**Program** I want the user to keep creating accounts, and then put them inside at list **Example o❔ POST request in swagger shows entire schema and parts of it are not needed in request bodyI have a one to one relationship between Product and CartItem, in the POST request for Product the C❔ ❔ Docker-Compose cannot find DockerfileHey! when i try to do docker-compose build i get this error : `failed to solve: rpc error: code = U