DharaPPatel
DharaPPatel

Reputation: 12743

Convert bool? to bool in View

i have checkbox in my which is like this @Html.CheckBoxFor(model => model.SKUs.Jewish) but my Jewish in database is nullable type so it gives me an error cannot implicitly convert type 'bool?' to 'bool'. how do check my model that it has values then it should show that else not.please help.

Upvotes: 6

Views: 6588

Answers (3)

Can Ürek
Can Ürek

Reputation: 661

And finally i solve that problem. If you are using a checkbox; uncheck Allow Nulls option of your field in your DB. That's it!

Upvotes: 0

hutchonoid
hutchonoid

Reputation: 33306

You could use the following:

@Html.CheckBox("SKUs.Jewish", Model.SKUs.Jewish.GetValueOrDefault());

If the value is not set it uses the nullable types default value which is false.

Upvotes: 6

Grant Thomas
Grant Thomas

Reputation: 45083

With a nullable type you check if it has a value with thing.HasValue, and get at the actual value using thing.Value.

Upvotes: 0

Related Questions