ASP.NET MVC 4 - Cannot perform runtime binding on a null reference
OK I am posting the full answer here -
Try
@
beforeif(@ViewBag.Stats[index] == null){
and remove@
from@ViewBag
inside theif
so that it look like this -@if(ViewBag.Stats[index] == null){
You are setting
index = 0
, insideforeach
, so it is initialised in every loop. Initialise it outsideforeach
like thisvar index = 0; foreach ...
if you are facing problem for the scope try this -
@{
var index = 0;
foreach (....) {
.......
index++
}
}