I am using html.checkbox in my mvc appplication , it will generate the html like
<input checked="checked" id="cb17" name="reports" type="checkbox" value="sdf" />
<input name="reports" type="hidden" value="false" />
<input checked="checked" id="cb18" name="reports" type="checkbox" value="ty" />
<input name="reports" type="hidden" value="false" />
when the form gets posted it is sending all the values of html.checkbox to mvc action through model binding concept.
reports[0]-"sdf"
reports[1]-"false"
reports[2]="ty"
reports[3]="false"
model:
public IEnumerable<string> reports{ get; set; }
i knew html.checkbox will create the hidden fields internally, but when i posting the form it need to send either one value of checkbox. why it is sending hidden fields too. how to avoid this.
referred the below link
asp.net mvc: why is Html.CheckBox generating an additional hidden input
but not able to get the proper understanding.
CheckBox()method is for binding to abooeanproperty, not astring(it correctly generates a<input type="checkbox" ... value="true" />and<input type="hidden" ... value="false" />which will correctly bind to abooleanproperty when the form is submitted. Do not useCheckBox()for binding to astring. You have not shown your model, or what your attempting to bind to, or the method your posting back to so its impossible to understand why you trying to use this hack.IEnumerable<string>(notbool) so just manually generate the html using<input type="checkbox" name="reports" value="sdf">id you want to receive an array of the values which have been checked.string Nameandbool IsSelectedand use aforloop to generate the controls - refer this answer for an example