简体   繁体   中英

how to generate check box using @HTML.CheckboxFor with bool?

In a form, I want to add a CheckBox to bind model value back to action. My model column is of type bool?

I tried the following code but getting error "Cannot implecitly convert type bool? to bool"

@Html.CheckBoxFor(m=>m.AccomPublic)

Please help me with correct way of using @HTML.CheckBoxFor.

Instead of CheckBoxFor use

@Html.EditorFor(m=>m.AccomPublic)

This will render a drop down with 3 values (True, False and Not Set)

This error is because of your property is of type nullable ie bool? Set your property as public bool nameofproperty{get;set;} And use @Html.checkboxFor(m=>m.nameofproperty)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM