Checkbox, Select: why the hidden value?

Feb 11, 2013 at 6:25 PM
Edited Feb 11, 2013 at 6:26 PM

I am using Teleriks HTML5 library KendoUI to add client side validation. I found that it wasn't working for select and checkbox controls because of the hidden tag that is there for each control (with the duplicate IDs). I removed the hidden tags being created on the RenderHtml() and then the problem was resolved.

Why even bother sending a hidden tag here? I see the comment ' add hidden tag, so that a value always gets sent for select tags', and I understand what happens - it will force the browser to send just a blank value to the server.. But I don't understand : why did you do this, and what does me removing it break?

Feb 11, 2013 at 11:23 PM
Browsers won't post a value for an unchecked checkbox or select. I think I was having model binding problems when I first wrote this library, and so I solved using hidden form elements.
Feb 19, 2013 at 5:52 PM
after commenting out the code in Control.RenderHtml() which creates the hidden tag, I noticed the whole control was now not rendering on posts.

After looking closer at the code, I saw what was going on.. See this:
            // add hidden tag, so that a value always gets sent for select tags
            //var hidden = new TagBuilder("input");
            //hidden.Attributes.Add("type", "hidden");
            //hidden.Attributes.Add("id", inputName + "_hidden");
            //hidden.Attributes.Add("name", inputName);
            //hidden.Attributes.Add("value", string.Empty);
            //html.Replace(PlaceHolders.Input, input.ToString() + hidden.ToString(TagRenderMode.SelfClosing));
Notice that last line - it appends to the HTML all of the input AND the hidden tag in a single line. If you simply add a new line of code to only append the input like so:
html.Replace(PlaceHolders.Input, input.ToString());
Then all is well - this was the only change needing to be made, although I did have to make this change for Select, Checkbox, and CheckBoxList. There's no need for this anymore and the bindings all work great.