I am using bootstrap-toggle along with the Html.CheckBoxFor() helper in a view like so:
@Html.CheckBoxFor(m => m.issueFlag,
new { id = "toggle-switch",
data_toggle = "toggle",
data_size = "normal",
data_on = "Issue on Invoice",
data_off = "No Issue on Invoice",
data_onstyle="danger",
data_offstyle="success",
data_width = "200" })
The switch appears and functions on the front end as expected, but when the form is submitted the value is always false. Other things in the form such as check boxes, drop downs and text boxes are captured correctly in the controller. The POST action in the controller is declared like:
[HttpPost]
[AuthorizeRights(Rights = "Viper_ViewAudit,Viper_EditAudit")]
public ActionResult ShowImage(PDFViewer pdfViewerModel, string Command)
{
... content ...
}
Am I doing something wrong somewhere? Why is m.issueFlag always false?
(I will provide more code needed, I am not sure what else is relevant).
Update The generated HTML is
<input data-off="No Issue on Invoice" data-offstyle="success"
data-on="Issue on Invoice" data-onstyle="danger" data-size="normal"
data-toggle="toggle" data-val="true"
data-val-required="The issueFlag field is required." data-width="200"
id="toggle-switch" name="issueFlag" type="checkbox"
value="true" />
<input name="issueFlag" type="hidden" value="false" />
I have now also tried doing a straight up input tag without using the helper with no change.
issueFlagtotruein the GET method before you pass the model to the view (it will now add thecheckedattribute) - does it now always pass back a value oftrueeven when you set it to 'off`'?falsein the POST method.issueFlaga property (with{ get; set; }) or just a field in your model?{ get; set; }methods.