title: MVC use Html.CheckBoxFor with nullable Bool
body: <p>I've got a checkbox that I want to display on my view related to a field called public, which basically says whether the particular row is public or not. In the database this is a bit field, but it allows nulls, due to how the table previously used to work.</p>
  
  <p>I'm using Html.CheckBoxFor but it is complaining about this field because in the system it is not a bool type, but rather a bool? type. What I want to do is have it so that if the field value is null, then it counts as a false on the front end (unfortunately updating the database values themselves is not an option).</p>
  
  <p>I have tried using the GetValueOrDefault, and putting a default value in my model file along the lines of:</p>
  
  <pre><code>public class Model
  {
      public bool? Public { get; set; }
  
      public SearchModel()
      { 
          Public = false;
      }
  }
  </code></pre>
  
  <p>however it was complaining about this, giving me the following error:</p>
  
  <pre><code>An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code
  
  Additional information: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
  </code></pre>
  
  <p>So i'm not sure how I can progress from here, can someone help point me in the right direction?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>This is the code on the view that i'm trying to use to show the checkbox. In this instance i'm adding some extra html attributes so that it appears as a toggle rather than a simple checkbox:</p>
  
  <pre><code>Html.CheckBoxFor(model =&gt; model.Public, new {data_toggle = "toggle", data_off = "No", data_on = "Yes", data_size = "small"})
  </code></pre>