I have model like this
public Nullable<bool> Space { get; set; }
public Nullable<bool> Lab { get; set; }
public Nullable<bool> Consultation { get; set; }
public Nullable<bool> Financial { get; set; }
public Nullable<bool> Other { get; set; }
in my view i initialize the checkbox as you can see here :
@Html.CheckBoxFor(model => model.Space.Value, new { id = "CheckSpace" })
@Html.CheckBoxFor(model => model.Lab.Value, new { id = "CheckLab" })
@Html.CheckBoxFor(model => model.Consultation.Value, new { id = "CheckConsultation"})
@Html.CheckBoxFor(model => model.Financial.Value, new { id = "CheckFinancial" })
@Html.CheckBoxFor(model => model.Other.Value, new { id = "CheckOther" })
but when i post the view to my controller all checkboxs value are null, could you please give some help?
EDIT
You should make the properties bool rather than bool? to bind them to @Html.CheckBoxFor. The helper only understands the values true or false. Then use
@Html.CheckBoxFor(model => model.Space, new { id = "CheckSpace" })
@Html.CheckBoxFor(model => model.Lab, new { id = "CheckLab" })
@Html.CheckBoxFor(model => model.Consultation, new { id = "CheckConsultation"})
@Html.CheckBoxFor(model => model.Financial, new { id = "CheckFinancial" })
@Html.CheckBoxFor(model => model.Other, new { id = "CheckOther" })
The helper generates both an
<input type="checkbox" name="Model Name" value="true" />
and
<input type="hidden" name="Model Name" value="false" />
such that a value is sent to the server when the checkbox is not checked.
You may also not want to set a specific id for each checkbox and instead let the framework generate one; You can get the id for a model property using, for example
@Html.IdFor(model => model.Space)
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