How to set bootstrap checkbox in razor CheckBoxFor()

后端 未结 5 1787
萌比男神i
萌比男神i 2021-01-06 01:25

I want to use bootstrap styled checkbox in my asp .net mvc form, however I find it very difficult. After hours looking for solution, I couldn\'t find anything that works for

5条回答
  •  粉色の甜心
    2021-01-06 01:39

    Under mvc a true/false checkbox based on the model value (boolean) also has a hidden field with the same name (supposedly to cater for when you don't check the box you still get a value in the Forms collection)

    Its this hidden field that stops the checkbox working in bootstrap format. A bootstrap styled checkbox from what I can see will never display properly under mvc using @Html.CheckBoxFor

    This is what i did to get it working

            

    Unfortunately the model isn't updated when you post the form and you have to handle that in the HttpPost'ed action

提交回复
热议问题