开发者

MVC3: EditorFor Behavior for a Boolean Variable

开发者 https://www.devze.com 2023-04-04 23:03 出处:网络
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 thi

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!


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消