Search code examples
angularaspnetboilerplate

Serialization of Checkbox values into Boolean values


I have some js that is trying to handle a checkbox value (assume not taken care of in "main.js")

var doctor = _$form.serializeFormToObject(); 
var mdoMipsCheckbox = $("input[name='MdoMips']:checked");
doctor.MdoMips = mdoMipsCheckbox.val();

the view looks like this:

<input type="checkbox" name="MdoMips" value="@Model.Doctor.MdoMips" class="filled-in" id="doctor-mdomips" @(Model.Doctor.MdoMips ? "true" : "") />

And the model for both Dto and Entity have the property:

[Column("MDO_MIPS")]
[DisplayName("MDO MIPS")]
public bool MdoMips { get; set; }

My problem is that my serialized object looks like:

BirthDate:"25-Dec-1962"
FirstName:"Philip"
Id:"4"
LastName:"Fergus"
MdoMips:**"on"**
MdoOther:"rrr"
Title:"Mr"

which fails validation.

I'm not sure what is the best way to fix this so that I see "true" or "false" values on the serialized boolean property.

Any help appreciated...


Solution

    1. Change public bool MdoMips to public byte MdoMips.
    2. Remove @(Model.Doctor.MdoMips ? "true" : "") from html, its not needed Angular does two way binding.