Search code examples
asp.net-mvc-2editorformodel

Why ASP.NET MVC 2 is rendering this code?


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 ?


Solution

  • 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.