Barcino
Barcino

Reputation: 13

Why does my checkbox return the wrong state?

I'm really confused. I have the following html in a form:

<input class="check-box" 
  id="Data__Correct" 
  name="Data.Correct" 
  type="checkbox" value="Data.Correct" />

This creates the following on the web page

 <input class="check-box" 
      id="Data__Correct" 
      name="Data.Correct" 
      type="checkbox" value="False" />

When I put a check in the checkbox, submit the form, and check with fiddler I see it's sending:

Data.Correct    False

I thought it should be the other way around. What's happening?

Upvotes: 1

Views: 704

Answers (1)

James Montagne
James Montagne

Reputation: 78630

You are misunderstanding how checkbox works. If the checkbox is unchecked then no value is passed to the backend. If the checkbox IS checked then the value in the value attribute is passed to the backend. In your case, you set value to False, so you are getting the string False not to be confused with the boolean value false.

If your intent with value='False' is to set the state of the checkbox on load, then you instead need to do this:

<input type="checkbox" ... checked/>

Or checked="checked" should also work I believe. If checked is present then the box is checked, otherwise it is unchecked.

Upvotes: 3

Related Questions