1

I am currently using javascript to generate HTML for checkboxes however when I go to post the form to my controller which accepts FormCollection as a parameter.

I then put these values into a dictionary and inspect the value within my dictionary. While other checkboxes only return one value, my autoDiscovery checkbox returns two which are 'on' and 'true', which doesn't make sense to me.

Here is the rendered HTML

<div class="form-group">
    <div class="col-md-10">
        <label class="col-md-3 control-label" for="autoDiscovery">
            <input id="autoDiscovery" type="checkbox" checked="" data-val="false" name="autoDiscovery">
            <input type="hidden" name="autoDiscovery" value="true"> Auto Discovery
        </label>
    </div>
</div>

and here is the javascript I am using to assign the checkboxes

function AssignCheckbox(variableName, jsonData, caption) {
var checked = jsonData == 1 ? 'checked' : '';
var value = jsonData == 1 ? 'true' : 'false';    
    return divFormGroupOpening + divOpeningInput +
            '<label class="col-md-3 control-label" for="' + variableName + '">' + '<input id="' + variableName + '" type="checkbox" name="' + variableName + '"  data-val="false"' + checked + '>' +
            '<input type="hidden" value="' + value + '" name="' + variableName + '">' +
            ' ' +  caption +'</label></div></div>';
        }
3
  • Don't check value, check the checked attribute. Commented Jul 2, 2015 at 9:36
  • Your check box does not have a value attribute - it needs to be value="True" and the hidden input needs to be value="False" Commented Jul 2, 2015 at 9:37
  • Why you have 2 inputs with the same names? Commented Jul 2, 2015 at 9:38

2 Answers 2

2

You have two inputs with the same exact name property. Ofcourse you would expect two values

<input id="' + variableName + '" name="' + variableName + '"  '>'
<input type="hidden" name="' + variableName + '">' 
Sign up to request clarification or add additional context in comments.

1 Comment

The thing is, why do my other checkboxes not return two values? I am using the exact same code to generate my HTML the only difference is the others are false where as autoDiscovery is true.
1

Inspect the html generated when you use the @Html.CheckBoxFor() method. It generates

<input type="checkbox" name="autoDiscovery" value="true" ..... />
<input type="hidden" name="autoDiscovery" value="false" />

You need to mimic that. Currently your checkbox element does not have a value attribute so if its checked in the view it will post back "on". And your hidden input value is "true" so that value posts as well.

Just as important, do not use FormCollection. Post back to a model, or add a parameter in the method - bool autoDiscovery so the DefaultModelBinder will correctly set the value to either true or false depending on the checked state of the checkbox

4 Comments

So should the hidden input type always be false then?
Yes it should be "false" and the checkbox value should be "true". This is by design. An unchecked checkbox does not post anything so in the controller you get false. But a checked checkbox does post back, so the values sent are autoDiscovery=true&autoDiscovery=false But the DefaultModelBinder reads only the first value (true) and ignores the second value.
I changed my value for the input field to false, and now the checkbox is true. However it is still posting two values true and false
You need to read my last comment. All form controls (that are not disabled) post back their values. The DefaultModelBinder will take care of binding the property correctly (in MVC you should never need to use FormCollection) - just include bool autoDiscovery a a parameter in your POST method and test it with both the checkbox checked and unchecked - it will be correctly bound. If you insist on using FormCollection then you need to parse the values yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.