title: How can I get CheckBox to maintain checked state across postback in ASP.NET MVC?
body: <p>I have an ASP.NET MVC project with a form. In the Action method that handles the POST verb I have a custom <code>IModelBinder</code> implementation that is binding the form data to my model instance.  If there are errors I am using <code>bindingContext.ModelState.SetAttemptedValue()</code> and <code>bindingContext.ModelState.AddModelError()</code> to persist the submitted value and error message to the <code>ModelState</code>.</p>
  
  <p>This works great and I can see the expected behavior occurring on my input controls that are rendered with <code>Html.TextBox()</code> (which calls through to <code>Html.InputHelper()</code>).  When I use <code>Html.CheckBox()</code> (which also calls through to <code>Html.InputHelper()</code>) the state of my CheckBox is <strong>NOT</strong> output to th <code>&lt;input /&gt;</code> tag.</p>
  
  <p>It seems to me like the <code>Html.InputHelper()</code> method is not using the AttemptedValue from the ModelState for input fields of type CheckBox.</p>
  
  <p>Here is the code from <a href="http://www.codeplex.com/aspnet/SourceControl/FileView.aspx?itemId=288010&amp;changeSetId=17272" rel="nofollow noreferrer">ASP.NET MVC <code>Html.InputHelper()</code> method</a>.</p>
  
  <p>Why is it that the CheckBox attemptedValue is not output to the input tag.  Is there something I am missing here or do I need to manually handle this case by checking the ModelState and setting the tag attribute myself?</p>
  
  <p><strong>Update 11/09</strong><br>
  Here is the call to <code>HtmlHelpers</code> that I am using to output the CheckBox:</p>
  
  <pre><code>&lt;%= Html.CheckBox("IsDerived") %&gt;
  </code></pre>
  
  <p>And here is the call that I am using to register the Attempted Value:</p>
  
  <pre><code>string isDerivedRequestValue = !string.IsNullOrEmpty(bindingContext.HttpContext.Request["IsDerived"]) ? bindingContext.HttpContext.Request.Form.GetValues("IsDerived") [0] : null;
  bindingContext.ModelState.SetAttemptedValue("IsDerived", isDerivedRequestValue);
  </code></pre>