Unchecked Checkbox Returning Null Value

Unchecked checkbox returning null value

If a checkbox is unchecked, it doesn't get sent, so setting it's value to 0 if it isn't checked isn't going to help - it will always return NULL.

There are two ways to fix this easily:

1) Assume a NULL in the PHP params means the checkbox is unchecked. If the checkbox doesn't always exist on the page, this could be problematic. By the sounds of it, there is a variable number of checkboxes, so this probably won't work.

2) Add a hidden input that has the same name as the checkbox with the value of 0 BEFORE the checkbox. If the checkbox is unchecked, the hidden field value will be used, if it is checked the checkbox value will be used.

<input type="hidden" name="checkbox_1" value="0">
<input type="checkbox" name="checkbox_1" value="1">

Note: If your names are in an array form (ie they have square brackets in them), this won't work, as the hidden fields will increment the array count as well.

Vue Checkbox Returning Null When Unckecked

Ah, the joys of checkboxes... Back in the day when we really did POST forms to the back end code, only checkboxes that were checked where sent in the POST request. If they were dynamically generated you had no idea which ones where in the DOM but not checked.

how about refactoring the code so that childFolders has a property of isSelected
then you can do <v-checkbox :checked="folder.isSelected" :ripple="false" :value="folder" @click="folder.isSelected = !folder.isSelected"/>

not tried it, just guessing.

My checkbox returns NULL when unchecked

It's simply because you have TargetNullValue=false on your Binding. That means a value of false from your CheckBoxValue property will be translated to null, which is what your converter sees.

set unchecked value of checkbox

Yes, using the default HtmlHelpers will achieve this for you

<%: Html.Checkbox("myCheckbox") %>

or with Razor

@Html.Checkbox("myCheckbox")

The Checkbox() method will render a input type="hidden" field along side the input type="checkbox" that will submit the value false when the checkbox is unchecked.

<input id="myCheckbox" name="myCheckbox" type="checkbox" value="true" />
<input name="myCheckbox" type="hidden" value="false" />

If you want to submit a value other than false then you should render the checkbox and hidden field yourself setting the value of the hidden field to your default value. Note that they must have the same name attribute.

HTML checkbox with [checked]=model?.value returns null when assigned to a model property

Well apparently I need to post a question to immediately figure it out... It seems that adding [ngModel] fixes the problem... Simple fix...

<input type="checkbox" formControlName="isBoolean" [ngModel]="model?.BooleanValue" [checked]="model?.BooleanValue" />


Related Topics



Leave a reply



Submit