3

I've noticed with checkboxes created with the EditorFor method that a hidden input always accompanies each checkbox created, and that its value is always set to FALSE. I've read a few posts about this on here, and understand that the hidden input is needed, because otherwise no information about the value of the checkbox will be sent at all for an unchecked box during a POST request. (I hope I've understood that correctly)

When I look at the Form Data coming out of a POST request, I get the strange (but expected) result that the value of a checked box is both TRUE and FALSE simultaneously: TRUE due to the box being checked, and FALSE due to the value in the hidden input. For example, on the variable "isActive" on one of my forms, I see this in the POST data:

isActive: true
isActive: false

So, which is it? Since it is true before it is false, wouldn't that mean that the value will always be false since the value of the hidden field will always overwrite the value of the checkbox? The order in which EditorFor places the input element and the hidden element (the hidden one first, and always set to false) just doesn't make any sense to me. Can someone explain the logic behind this to me?

Thanks!

1 Answer 1

2

Editor for model puts them the right way around so that the value of the input box will overwrite the value of the checkbox if it is checked during the mapping. Its not really very mysterious?

Sign up to request clarification or add additional context in comments.

4 Comments

That would make sense, but it seems to be the other way around. The checkbox renders first, then the hidden input. The POST data also comes out in that order. Since the hidden is always set to false, then it seems that the value for the field will ultimately always be false. Unless true + false = true (1 + 0 =1)...?
I believe that for a single item it gets the first one, so the second one is ignored. You can test this by putting multiple editors for the same text input on one page and see which value gets used in the post.
The default model binder will ignore additional values beyond the first for non-collection typed properties.
looking at the MVC source for DefaulModelBinder here aspnet.codeplex.com/SourceControl/changeset/view/70178#266460 I believe its because it runs from the model and finds properties in the request that match the name.

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.