vendredi 23 octobre 2015

Bootstrap switches return "on/off" instead of "true/false"

Bootstrap has massive usage, and lots of people much smarter than me have worked on it, so I'm sure the issue I'm about to point out is a total non issue and I'm just using it wrong, but....

I have a form with a checkbox turned into a switch using bootstrapSwitch()

here's my html

<div class="bootstrap-switch-square">
   <input type="checkbox" data-toggle="switch" name="Resend" id="Resend" />
</div>

And js:

$("#Resend").bootstrapSwitch();

This produces a neat bootstrap toggle switch on my page. The page posts to an ASP.NET MVC4 controller method, which expects a boolean in the model.

public class MyModel
{
    public bool Resend { get; set; }
}

When I post this form, my Resend boolean is always false. Why? Because bootstrap switch posts a value of on rather than true.

I don't want to change my model from a boolean to a string, so I'm forced to intercept the post in jquery and change the value of my checkbox to true if it's on. This seems backwards to me. Surely if bootstrapSwitch() is used on a checkbox it should produce true/false instead of on/off

Am I missing something?




Aucun commentaire:

Enregistrer un commentaire