1

I have a code on server side:

@Html.EditorFor(m => m.RememberMe)

RememberMe is a boolean field of my model. And the rendered HTML:

<input class="check-box" data-val="true" data-val-required="The Remember Me ? field is required." id="RememberMe" name="RememberMe" type="checkbox" value="true" />
<input name="RememberMe" type="hidden" value="false" />

There is a hidden field with the same name="RememberMe". I don't know why and what is the purpose of this hidden field.

When I debug on server side with the checkbox checked, the model was mapped correctly. I got: myModel.RememberMe = true. But when I inspected the Request["RememberMe"]. I saw "true,false". The false must be from the hidden field as they have same name.

My questions are:

  • Will the false cause problems?
  • Why asp.net mvc renders a hidden field like this?
  • If the hidden field is not neccesary. How can I get rid of it? I think it's best to get only "true" for Request["RememberMe"].

Thank you

Khanh TO
  • 48,509
  • 13
  • 99
  • 115
  • 1
    I think this is answered [here](http://stackoverflow.com/questions/11011300/asp-net-mvc-3-checkboxfor-method-outputs-hidden-field-that-hidden-field-value) – bazz Jul 03 '13 at 03:59

1 Answers1

1

Will the false cause problems?

the false will not cause any problems but since you rendering not null that means that your RememberMe is set to bool and not nulabble bull therefore if the answer is not checked it will be false

Why asp.net mvc renders a hidden field like this?

because if you look at your html you can not have two values it either true or false but since null is not the answer hidden field was added to hold the value of false value="false"

If the hidden field is not neccesary. How can I get rid of it? I think it's best to get only "true" for Request["RememberMe"].

unless you really think it a problem you might gone have to write your own check-box helper because as far as i know check-box helper that used will not accept the null-able value as input

COLD TOLD
  • 13,513
  • 3
  • 35
  • 52