title: model item passed into the dictionary is of type 'System.Boolean', but this dictionary requires a model item of type 'MyProject.checkboxstate'
body: <p>I have a custom html helper with the last parameter as bool type. My code works fine when there is data in the database. But, without data, it throws following error:</p>
  
  <blockquote>
    <p>System.NullReferenceException: Object reference not set to an instance of an object.</p>
  </blockquote>
  
  <p>Then, I tried to check for the possible 'null value' and send the value as 'false' to the view, apparently, without success.</p>
  
  <pre><code>public ActionResult Index()
  {            
      bool? defaultVal = true;
  
      var dbValue = context.checkboxstates.Where(c =&gt; c.Name == "Country").FirstOrDefault();
      if (dbValue == null)
      {
          return View(defaultVal);
      }               
      return View(dbValue);
  }     
  </code></pre>
  
  <p>My View:</p>
  
  <pre><code>@model MyProject.CheckboxState
  @Html.MyCustomHtmlHelper("text", "AnotherText", "changeState", Model.state)
  </code></pre>
  
  <p>Here, Model.state is responsible for 'checking' checkbox 'checked' or 'unchecked' based upon the value returned from database. I do not have a model as I'm using database first approach.</p>
  
  <p>If I try to pass explicitly a boolean value from the controller, I get this error message:</p>
  
  <blockquote>
    <p>The model item passed into the dictionary is of type 'System.Boolean', but this dictionary requires a model item of type 'MyProject.checkboxstate'.</p>
  </blockquote>