Davit Tvildiani
Davit Tvildiani

Reputation: 1965

Asp.Net MVC Razor Foreach throws null Exception

My code :

 @if (Model.ClientTypes != null)
 {
  foreach (var clientType in Model.ClientTypes)
  {
     @:blabla :)
  }
 }

ClientTypes collection is a list and it´s not null, there are 5 elements in ClientTypes, razor engine enters in loop and after looping all 4 elements at the end throws null exception :(

what is wrong ?

enter image description here

Upvotes: 2

Views: 3249

Answers (1)

angiestardust
angiestardust

Reputation: 61

I ran into this same problem and discovered that the error was actually lying to me. The error wasn't on the foreach loop, it was on the line following the foreach loop. So my recommendation is to comment out anything that could be null immediately following the foreach loop and then uncomment one at a time until you find the culprit. Good luck!

Upvotes: 5

Related Questions