views:

211

answers:

2

I've got problem with binding data type boolean to checkbox in MVC 2 data annotations Here's my code sample:

label>
Is Hot
</label>
<%=Html.CheckBoxFor(model => model.isHot, new {@class="input" })%>

It always raise this error message below at (model=>model.isHot).

Cannot convert lambda expression to delegate type 'System.Func<Framework.Models.customer,bool>' because some of the return types in the block are not implicitly convertible to the delegate return type

Cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?)

Please suggest me how can I solve this problem?

Thanks in advance.

+2  A: 
<%=Html.CheckBoxFor(model => model.isHot ?? false, new {@class="input" })%>

I think the code above will work for you

anilca
I've tried this situation .It passes on building time but it raises the same error message on runtime.
nvtthang
<%=Html.CheckBoxFor(model => model.isHot.HasValue ? model.isHot.Value : false, new {@class="input" })%>
anilca
A: 

Try (model=>(bool)model.isHot)

Koen Stroobants