问题
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
回答1:
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; }
回答2:
@Html.EditorFor(model => model.IsPhonePublic)
@Html.Label('Don't Show my number.')
来源:https://stackoverflow.com/questions/24446828/razor-checkbox-not-binding-to-model