You are currently viewing Convert type ‘bool?’ to ‘bool’ for a Html.CheckBoxFor

Convert type ‘bool?’ to ‘bool’ for a Html.CheckBoxFor

A checkbox needs to have a value (After all it can only be in one of two states, checked or unchecked true or false). Why can’t you just make isActive a bool since it’s just the view model. In your model you can keep isActive as a nullable. When you create the view model just check to see if the model’s isActive is null and if so set the view model’s isActive to false.

Another alternative is to not use the strongly typed helper which expects a bool, and just use Html.CheckBox like so


<%:Html.CheckBox("IsActive", Model.IsActive.HasValue? Model.IsActive : false)%>

Resources

https://forums.asp.net/t/1794010.aspx?convert+type+bool+to+bool+for+a+Html+CheckBoxFor