When i create a Html.CheckBox() in Asp.net MVC 2 i wonder there is a hidden field with that checkbox as well when i view its html, from where that hidden field is coming and what its purpose is?
Asked
Active
Viewed 938 times
1
-
Does this answer your question? [asp.net mvc: why is Html.CheckBox generating an additional hidden input](https://stackoverflow.com/questions/2697299/asp-net-mvc-why-is-html-checkbox-generating-an-additional-hidden-input) – James Skemp Jul 01 '21 at 18:48
1 Answers
4
From a comment in the ASP.NET MVC source code:
if (inputType == InputType.CheckBox) {
// Render an additional <input type="hidden".../> for checkboxes. This
// addresses scenarios where unchecked checkboxes are not sent in the request.
// Sending a hidden input makes it possible to know that the checkbox was present
// on the page when the request was submitted.
StringBuilder inputItemBuilder = new StringBuilder();
inputItemBuilder.Append(tagBuilder.ToString(TagRenderMode.SelfClosing));
TagBuilder hiddenInput = new TagBuilder("input");
hiddenInput.MergeAttribute("type", HtmlHelper.GetInputTypeString(InputType.Hidden));
hiddenInput.MergeAttribute("name", name);
hiddenInput.MergeAttribute("value", "false");
inputItemBuilder.Append(hiddenInput.ToString(TagRenderMode.SelfClosing));
return inputItemBuilder.ToString();
}
For example if the user doesn't check the value there's nothing sent to the server so if you are binding to some view model in your post action there won't be any value. The hidden field sends the value of false
.

Darin Dimitrov
- 1,023,142
- 271
- 3,287
- 2,928
-
3In my opinion this is stupid. I've got a GET form and it's passing through conflicting paramaters. I.e. myField=true&myfield=false. Oh well back to pure HTML – BritishDeveloper Aug 13 '11 at 09:46