title: @foreach model is null :System.NullReferenceException: Object reference not set to an instance of an object
body: <p>I have this error on read my view on @foreach.</p>
  
  <p>Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Model  is null.</p>
  
  <p>this is my userprofile model.</p>
  
  <pre><code> public class UsersContext : DbContext
  {
  
      public UsersContext()
          : base("DefaultConnection")
      {
      }
  
      public DbSet&lt;UserProfile&gt; UserProfiles { get; set; }
      public DbSet&lt;Secretarias&gt; Secretarias { get; set; }
      public DbSet&lt;Secretarias_Direcciones&gt; Secretarias_Direcciones { get; set; }
  }
  
  [Table("UserProfile")]
  public class UserProfile
  {
      [Key]
      public int UserId { get; set; }
      [Required]
      public string Nombre { get; set; }
      [Required]
      public int SecretariaId { get; set; }
      [Required]
      public int DireccionId { get; set; }
      [Required]
      public string UserName { get; set; }
  
  }
  </code></pre>
  
  <p>this  is my control</p>
  
  <pre><code>public ActionResult ListaUsuarios()
  {
      UsersContext db = new UsersContext();
      var model = from usrs in db.UserProfiles
                  select new { usrs.UserId, usrs.Nombre, usrs.UserName, usrs.SecretariaId, usrs.DireccionId };
      ViewBag.Model = model.ToList();
  
      return View();
  }
  </code></pre>
  
  <p>this is my view</p>
  
  <pre><code> @foreach (var item in Model)
  {
      &lt;tr&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.UserId)
          &lt;/td&gt;
           &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.Nombre)
          &lt;/td&gt;
                  &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.UserName)
          &lt;/td&gt;
                  &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.SecretariaId)
          &lt;/td&gt;
                  &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.DireccionId)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.ActionLink("Edit", "Edit", new { id=item.UserId }) |
              @Html.ActionLink("Details", "Details", new { id=item.UserId}) |
              @Html.ActionLink("Delete", "Delete", new { id=item.UserId })
          &lt;/td&gt;
      &lt;/tr&gt;
  }
  </code></pre>
  
  <p>Here are the Exception Details:</p>
  
  <pre><code>System.NullReferenceException: Object reference not set to an instance
  of an object. Model is null.
  </code></pre>