views:

39

answers:

1

I have an bool property in a class. And using <%= Html.EditorForModel() %> its generating this code:

<div class="editor-field">
  <input class="check-box" id="Ativo" name="Ativo" type="checkbox" value="true">
  <input name="Ativo" type="hidden" value="false">
</div>

My question is: why it's creating an input hidden ?

+3  A: 

It's because when you submit a form, unless the checkbox is checked, it will not be submitted to the server in the postback. it helps distinguish between a false value and a missing value. They are just working around one of the vagaries of the way forms work on the web.

Malevolence
That's quite interesting
SLC
@Malevolence Thank you!
Zote
So an input type=checkbox has a priority over an input type=hidden when they both have the same name or is it because the checkbox is first in the DOM?
ZippyV
It's because they have the same name and the first one takes priority.
Malevolence