0

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.

3
  • #BisData is form tag id? Commented May 9, 2012 at 10:46
  • k.. u add alert($("#BisData").serialize()); put this output here... Commented May 9, 2012 at 11:02
  • It's the two rows of code I've got in the question, BeslutIStort.Ok=&BeslutIStort.Ok=false and BeslutIStort.Ok=false. It also contains other data, but that is normal. Commented May 9, 2012 at 12:17

1 Answer 1

1

it aint that strange either..
first of all you have recieved the value two times BeslutIStort.Ok=&BeslutIStort.Ok=false because Razor engine automatically puts a hidden field with the same name and value false inside the form when ever you use @Html.CheckboxFor()
this is done so that even if the checkbox isnt checked a false value is returned.

second... you are recieveing null value because there is no value on your input(checkbox) html control. if you want that a value should be returned just add one with the following code

<div id="colOk" class="kolumn_header"> @Html.LabelFor(model => model.BeslutIStort.Ok) @Html.CheckBoxFor(model => model.BeslutIStort.Ok, new { @class = "rensa", tabindex = 5,value="true" }) </div> 
Sign up to request clarification or add additional context in comments.

6 Comments

It changes the generated html, to <input class="rensa" id="BeslutIStort_Ok" name="BeslutIStort.Ok" tabindex="5" type="checkbox" value="true" /><input name="BeslutIStort.Ok" type="hidden" value="false" /> , but it's unfortunately the same when serializing it.
u mean you are something like this...BeslutIStort.Ok=&BeslutIStort.Ok=false
Yes, when checked. When not check, it's BeslutIStort.Ok=false only
i mean when its checked now arnt u getting BeslutIStort.Ok=true&BeslutIStort.Ok=false
No, I was expecting that, it's still BeslutIStort.Ok=&BeslutIStort.Ok=false (one empty and one false)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.