0
votes

In my model i have the following property.

public bool? ShowDocumentNumber { get; set; }

when i try to show it on the razor view

@Html.CheckBoxFor(model =>model.ShowDocumentNumber)

I get the following error.

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

when i try to cast it

@Html.CheckBoxFor(model =>bool.Parse(model.ShowDocumentNumber.ToString()))

i get the following error at run time.

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

How can i display the checkbox on the razor view?

1
Check box should have only two values true or false, checked or unchecked. It shouldn't be nullablessilas777

1 Answers

0
votes

I think you need to first check if the nullable bool has a value

@if (Model.ShowDocumentNumber.HasValue)
{
    @Html.CheckBoxFor(model => model.ShowDocumentNumber.Value)
}