I have a very strange case, where I post a value from a checkbox to my controller, it's not true, even when checked!
Part of my viewModel:
public class BeslutIStortViewDTO { public BeslutIStortDTO BeslutIStort { get; set; } public BeslutIStortListorDTO SelectListor { get; set; } } public class BeslutIStortDTO { public int id { get; set; } public bool Ok { get; set; } ... other stuff } part of my view:
<div id="colOk" class="kolumn_header"> @Html.LabelFor(model => model.BeslutIStort.Ok) @Html.CheckBoxFor(model => model.BeslutIStort.Ok, new { @class = "rensa", tabindex = 5 }) </div> In the string passed to the controller, when the box in unchecked, everything seems ok:
BeslutIStort.Ok=false BUT! When I check it:
BeslutIStort.Ok=&BeslutIStort.Ok=false Its twice in the string, the first has no value, the oterh is false, so both are wrong. I would of course expect only one, and that one to be true.
Another interesting finding is that in the acutal rendered html, the checkbox looks like this:
<input class="rensa" id="BeslutIStort_Ok" name="BeslutIStort.Ok" tabindex="5" type="checkbox" value="true" /><input name="BeslutIStort.Ok" type="hidden" value="false" /> It has a hidden field!
When I post to the values to the server, I do it with ajax, and the data parameter is defined like this:
data: $("#BisData").serialize(), I hope someone could help me to shed some light into this, and help me getting some "true" to my controller :-)
=== S O L V E D === As it tured out, I had javascript like this:
$(".rensa").val("");
Which made the checkbox non functional. I added this:
$(".rensaCheckbox").attr("checked", false);
for the checkboxes, so now it works.