title: MVC NullReferenceException on @foreach (var item in Model)
body: <p>I'm getting the below error, after reading through this </p>
  
  <p><a href="https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it">What is a NullReferenceException, and how do I fix it?</a> </p>
  
  <p>I understand what the error is but not how its being caused or how to fix it in my case, </p>
  
  <p>Can anyone explain why? </p>
  
  <p>Error:</p>
  
  <p>System.NullReferenceException: Object reference not set to an instance of an object.</p>
  
  <p>Error on line:</p>
  
  <p>@foreach (var item in Model)</p>
  
  <p>Controller: </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using System.Web.Security;
  using TMTMonitorandCompare.Models;
  
  
  namespace TMTMonitorandCompare.Controllers
  {
  
  public class HomeController : Controller
  
  {
   private MetaClone_2Entities db = new MetaClone_2Entities();
   public ActionResult Index(string filtername)        
      {
  
          var filterresults = from m in db.UserInfoes
                              select m;            
  
          filterresults = filterresults.Where(x =&gt; x.UserCode.ToString().Contains(filtername)).OrderBy(x =&gt; x.UserCode);
  
          return View(filterresults);
      }
      public ActionResult Login()
      {
          return View("Login");
      }
      public ActionResult CheckUser()
      {
          //check username &amp; password
          if ((Request.Form["username"] == "user") &amp;&amp; (Request.Form["password"] == "pass"))
          {
              // use forms auth class to set the cookie
              FormsAuthentication.SetAuthCookie(Request.Form["username"], true);
              // redirect to view
              return View("Index");
          }
          else
          {
              return View("Login");
          }
      }
  
      public ActionResult SessionTimeout()
      {
          return View();
      }
      public ActionResult ForgotPassword()
      {
          return View();
      }
    }
  }
  </code></pre>
  
  <p>View: </p>
  
  <pre><code>@model IEnumerable&lt;TMTMonitorandCompare.Models.UserInfo&gt;
  @{
  ViewBag.Title = "Home Page";
  Layout = "~/Views/Shared/_Layout.cshtml";
  }
  &lt;script&gt;
  $(document).ready(function () {
      @*Validation for Text fields with name formpart*@
      $('#SendRequest').click(function (e) {
          var isValid = true;
          $("input[type='text'][name='formpart']").each(function () {
              if ($.trim($(this).val()) == '') {
                  isValid = false;
                  $(this).css({
                      "border": "1px solid red",
                      "background": "#FFCECE"
                  });
              }
              else {
                  $(this).css({
                      "border": "",
                      "background": ""
                  });
              }
          });
          if (isValid == false)
              e.preventDefault();
          else
              alert('Thank you for submitting');
      });
  
  
      $("#resultsgo").click(function () {
          $("#basicModal2").modal('hide');
      });
      $("#bckpage").click(function () {
          $("#basicModal3").modal('hide');
          $("#basicModal2").modal('show');
      });
      $("#bcktostart").click(function () {
          $("#basicModal3").modal('hide');
      });
  
      $("#SendRequest").click(function () {
          var imei = ("IMEI: " + $("#imei").val());
          $('#printImei').html(imei);
          var phonenumber = ("Phone Number: " + $("#phoneNumber").val());
          $('#printPhoneNumber').html(phonenumber);
          var policynumber = ("Policy Number: " + $("#policyNumber").val());
          $('#printPolicyNumber').html(policynumber);
      });
  
      @*Code for passing code (Should work)*@
      $("#SendCodeRequest").click(function () {
          var thecode = ("Code: " + $("#theCode").val());
          $('#printCode').html(thecode);
      });
  
  
      @*Code for dropdowns ( not working)*@
      $('#pickButton').dropdown();
      $('#selectionDropdown li').on('click', function () {
          $('#dropdown_title').html($(this).find('a').html());
      });
  
  });
  &lt;/script&gt;
  
  
  &lt;button type="button" class="btn btn-success pull-right" id="logonbutton" onclick="location.href = '@Url.Action("Logon", "Home")'"&gt;Logon&lt;/button&gt;
  &lt;table class="table"&gt;
  &lt;thead&gt;
      &lt;tr&gt;
          &lt;th&gt;New Request&lt;/th&gt;
          &lt;th&gt;Existing Request&lt;/th&gt;
      &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;form class="form-horizontal"&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label class="control-label col-xs-2" for="imei"&gt;IMEI:&lt;/label&gt;
                      &lt;div class="col-xs-9"&gt;
                          &lt;input type="text" class="form-control" id="imei" name="formpart" placeholder="IMEI"&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
  
                  &lt;div class="form-group"&gt;
                      &lt;label class="control-label col-xs-2" for="phoneNumber"&gt;Phone Number:&lt;/label&gt;
                      &lt;div class="col-xs-9"&gt;
                          &lt;input type="tel" class="form-control" id="phoneNumber" placeholder="Phone Number"&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
  
                  &lt;div class="form-group"&gt;
                      &lt;label for="platform" class="control-label col-xs-2"&gt;Policy Organisation:&lt;/label&gt;
                      &lt;div class="col-xs-10"&gt;
                          &lt;div class="dropdown"&gt;
                              &lt;button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown"&gt;
                                  Select
                                  &lt;span class="caret"&gt;&lt;/span&gt;
                              &lt;/button&gt;
                              &lt;ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu2"&gt;
                                  &lt;li role="presentation"&gt;&lt;a role="menuitem" tabindex="-1" href="#"&gt;TMT Internals&lt;/a&gt;&lt;/li&gt;
                              &lt;/ul&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
  
                  &lt;div class="form-group"&gt;
                      &lt;label for="policyNumber" class="control-label col-xs-2"&gt;Policy Number:&lt;/label&gt;
                      &lt;div class="col-xs-10"&gt;
                          &lt;input type="text" class="form-control" id="policyNumber" name="formpart" placeholder="Policy Number"&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
  
                  &lt;div class="form-group"&gt;
                      &lt;label for="platform" class="control-label col-xs-2"&gt;Platform:&lt;/label&gt;
                      &lt;div class="col-xs-10"&gt;
                          &lt;div class="dropdown"&gt;
                              &lt;button class="btn btn-default" id="pickButton" data-toggle="dropdown"&gt;
                                  &lt;span id="dropdown_title"&gt;Select&lt;/span&gt;
                                  &lt;span class="caret"&gt;&lt;/span&gt;
                              &lt;/button&gt;
                              &lt;ul class="dropdown-menu" id="selectionDropdown"&gt;
                                  &lt;li&gt;&lt;a tabindex="-1" href="#"&gt;Android&lt;/a&gt;&lt;/li&gt;
                                  &lt;li&gt;&lt;a tabindex="-1" href="#"&gt;IOS&lt;/a&gt;&lt;/li&gt;
                                  &lt;li&gt;&lt;a tabindex="-1" href="#"&gt;Windows Phone&lt;/a&gt;&lt;/li&gt;
                              &lt;/ul&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
  
                  &lt;div class="form-group"&gt;
                      &lt;label for="diagnosticMode" class="control-label col-xs-2"&gt;Diagnostic Mode:&lt;/label&gt;
                      &lt;div class="col-xs-10"&gt;
                          &lt;div class="dropdown"&gt;
                              &lt;button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown"&gt;
                                  Select
                                  &lt;span class="caret"&gt;&lt;/span&gt;
                              &lt;/button&gt;
                              &lt;ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu3"&gt;
                                  &lt;li role="presentation"&gt;&lt;a role="menuitem" tabindex="-1" href="#"&gt;Simple&lt;/a&gt;&lt;/li&gt;
                                  &lt;li role="presentation"&gt;&lt;a role="menuitem" tabindex="-1" href="#"&gt;Advanced&lt;/a&gt;&lt;/li&gt;
                                  &lt;li role="presentation"&gt;&lt;a role="menuitem" tabindex="-1" href="#"&gt;Mannual&lt;/a&gt;&lt;/li&gt;
                                  &lt;li role="presentation"&gt;&lt;a role="menuitem" tabindex="-1" href="#"&gt;Complete&lt;/a&gt;&lt;/li&gt;
                              &lt;/ul&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
  
                  &lt;div class="form-group"&gt;
                      &lt;div class="col-xs-offset-2 col-xs-10"&gt;
                          &lt;a id="SendRequest" href="#" class="btn btn-success"
                             data-toggle="modal"
                             data-target="#basicModal"&gt;Create New Request&lt;/a&gt;
                          &lt;button type="submit" class="btn btn-primary"&gt;Back to List&lt;/button&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/form&gt;
          &lt;/td&gt;
  
          &lt;td&gt;
              &lt;div class="form-group"&gt;
                  &lt;div class="col-xs-offset-2 col-xs-10"&gt;
                      &lt;div class="span7 text-center"&gt;
                          &lt;input type="text" class="form-control" id="theCode" placeholder="Please Enter Code"&gt;
                          &lt;input type="submit" value="Go!" class="btn btn-success" id="sendcoderequest" data-toggle="modal"
                                 data-target="#basicModal2" /&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              @using (Html.BeginForm("Index", "Home", FormMethod.Get))
              {
                  &lt;p&gt;
                      &lt;label for="platform" class="control-label"&gt;Enter Code:&lt;/label&gt;&lt;br /&gt;
                      @Html.TextBox("filtername")
                      &lt;input type="submit" value="Filter" /&gt;
                  &lt;/p&gt;
              }
  
              &lt;div class="modal fade" id="basicModal" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true"&gt;
                  &lt;div class="modal-dialog"&gt;
                      &lt;div class="modal-content"&gt;
  
                          &lt;div class="modal-header"&gt;
                              &lt;button type="button" class="close" data-dismiss="modal" aria-hidden="true"&gt;&amp;amp;times;&lt;/button&gt;
                              &lt;h4 class="modal-title" id="myModalLabel"&gt;Summary&lt;/h4&gt;
                          &lt;/div&gt;
  
                          &lt;div class="modal-body"&gt;
                              &lt;span id="printImei"&gt;&lt;/span&gt;&lt;br /&gt;
                              &lt;span id="printPhoneNumber"&gt;&lt;/span&gt;&lt;br /&gt;
                              &lt;span id="printPolicyNumber"&gt;&lt;/span&gt;
  
                          &lt;/div&gt;
  
                          &lt;div class="modal-footer"&gt;
                              &lt;button type="button" class="btn btn-default" data-dismiss="modal"&gt;Close&lt;/button&gt;
                              &lt;button type="button" class="btn btn-primary"&gt;Save changes&lt;/button&gt;
                          &lt;/div&gt;
  
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
  
              &lt;div class="modal fade" id="basicModal2" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true"&gt;
                  &lt;div class="modal-dialog"&gt;
                      &lt;div class="modal-content"&gt;
                          &lt;div class="modal-header"&gt;
                              &lt;button type="button" class="close" data-dismiss="modal" aria-hidden="true"&gt;&amp;amp;times;&lt;/button&gt;
                              &lt;h4 class="modal-title" id="myModalLabel"&gt;Summary&lt;/h4&gt;
                          &lt;/div&gt;
                          &lt;div class="modal-body"&gt;
                              &lt;h2&gt;Results&lt;/h2&gt;
  
                              &lt;span id="printCode"&gt;&lt;/span&gt;&lt;br /&gt;
  
                              &lt;div class="pull-right"&gt;&lt;button type="submit" class="btn btn-success" id="toggle"&gt;Toggle&lt;/button&gt; &lt;/div&gt;
  
                              &lt;table class="table"&gt;
                                  &lt;thead&gt;
                                      &lt;tr&gt;
                                          &lt;th&gt;&lt;/th&gt;
                                          &lt;th&gt;Date&lt;/th&gt;
                                          &lt;th&gt;Test Type&lt;/th&gt;
                                      &lt;/tr&gt;
                                  &lt;/thead&gt;
                                  &lt;tbody&gt;
  
  
                                      @foreach (var item in Model)
                                      {
  
                                          &lt;tr&gt;
                                              &lt;td&gt;
                                                  &lt;input type="checkbox"&gt;
                                              &lt;/td&gt;
                                              &lt;td&gt;
                                                  @Html.DisplayFor(modelItem =&gt; item.CreationDateTime)
                                              &lt;/td&gt;
                                              &lt;td&gt;
                                                  @Html.DisplayFor(modelItem =&gt; item.AppModeId)
                                              &lt;/td&gt;
                                          &lt;/tr&gt;
                                      }
  
                                  &lt;/tbody&gt;
                              &lt;/table&gt;
                              &lt;div class="form-group"&gt;
                                  &lt;div class="col-xs-offset-2 col-xs-10"&gt;
                                      &lt;a href="#" class="btn btn-success"
                                         data-toggle="modal"
                                         data-target="#basicModal3" id="resultsgo"&gt;Go!&lt;/a&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                          &lt;div class="modal-footer"&gt;
                              &lt;button type="button" class="btn btn-success"&gt;Save changes&lt;/button&gt;
                              &lt;button type="button" class="btn btn-primary" data-dismiss="modal"&gt;Close&lt;/button&gt;
  
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
  
              &lt;div class="modal fade modal-lg" id="basicModal3" tabindex="-1" role="dialog" aria-labelledby="basicModal3" aria-hidden="true"&gt;
                  &lt;div class="modal-dialog modal-lg"&gt;
                      &lt;div class="modal-content"&gt;
  
                          &lt;div class="modal-header"&gt;
                              &lt;button type="button" class="close" data-dismiss="modal" aria-hidden="true"&gt;&amp;amp;times;&lt;/button&gt;
                              &lt;h4 class="modal-title" id="myModalLabel"&gt;Summary&lt;/h4&gt;
                          &lt;/div&gt;
  
                          &lt;div class="modal-body"&gt;
                              &lt;h2 id="tableheading"&gt;Comparison&lt;/h2&gt;
                              &lt;div class="btn-toolbar"&gt;
                                  &lt;div class="btn-group pull-left"&gt;
                                      &lt;button type="button" class="btn btn-success" id="bcktostart"&gt;Back to Start&lt;/button&gt;
                                      &lt;button type="button" class="btn btn-success" id="bckpage"&gt;Back a Page&lt;/button&gt;
                                  &lt;/div&gt;
                                  &lt;div class="btn-group pull-right"&gt;
                                      &lt;button type="button" class="btn btn-success" id="singlebutton"&gt;Single&lt;/button&gt;
                                      &lt;button type="button" class="btn btn-success" id="multibutton"&gt;Multi&lt;/button&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                              &lt;script&gt;
                                  $(document).ready(function () {
                                      $("#Table1").hide()
                                      $("#Table2").hide()
                                  });
                                  $("#multibutton").click(function () {
                                      $("#Table2").hide()
                                      $("#Table1").show()
                                      $("#tableheading").text('Multi-Comparision');
                                  });
                                  $("#singlebutton").click(function () {
                                      $("#Table1").hide()
                                      $("#Table2").show()
                                      $("#tableheading").text('Single-Comparision');
                                  });
                              &lt;/script&gt;
  
                              &lt;table class="table table-striped" id="Table1"&gt;
                                  &lt;thead&gt;
                                      &lt;tr&gt;
                                          &lt;th&gt;Phone 1&lt;/th&gt;
                                          &lt;th&gt;Phone 2&lt;/th&gt;
                                          &lt;th&gt;Phone 3&lt;/th&gt;
                                      &lt;/tr&gt;
                                  &lt;/thead&gt;
                                  &lt;tbody&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;***Result***&lt;/td&gt;
                                          &lt;td&gt;***Result***&lt;/td&gt;
                                          &lt;td&gt;***Result***&lt;/td&gt;
                                      &lt;/tr&gt;
                                  &lt;/tbody&gt;
                              &lt;/table&gt;
                              &lt;table class="table table-striped" id="Table2"&gt;
                                  &lt;thead&gt;
                                      &lt;tr&gt;
                                          &lt;th&gt;Phone 1&lt;/th&gt;
                                      &lt;/tr&gt;
                                  &lt;/thead&gt;
                                  &lt;tbody&gt;
                                      &lt;tr&gt;
                                          &lt;td&gt;***Result***&lt;/td&gt;
                                      &lt;/tr&gt;
                                  &lt;/tbody&gt;
                              &lt;/table&gt;
                          &lt;/div&gt;
                          &lt;div class="modal-footer"&gt;
                              &lt;button type="button" class="btn btn-success"&gt;Save changes&lt;/button&gt;
                              &lt;button type="button" class="btn btn-primary" data-dismiss="modal"&gt;Close&lt;/button&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/tbody&gt;
  </code></pre>
  
  <p></p>