I'm an asp.net mvc newbie. I have a checkbox in my form
@Html.CheckBox("Don't show my number", Model.IsPhonePublic)
But whether I check the box or not the Model.IsPhonePublic
is always false while submitting the form. Any pointers
You are using the helper wrong, See definition here :
So you do this:
@Html.Label("Don't show my number")
@Html.CheckBox("IsPhonePublic", Model.IsPhonePublic)
or
@Html.Label("Don't show my number")
@Html.CheckBoxFor(m => m.IsPhonePublic)
or third and clean solution:
@Html.LabelFor(m => m.IsPhonePublic)
@Html.CheckBoxFor(m => m.IsPhonePublic)
And in you model definition:
[DisplayName("Don't show my number")]
public bool IsPhonePublic { get; set; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With