0

runtime error: cannot implicitly convert type 'bool?' to bool. An explicit conversion exists (are you missing a cast?)

sql database type = bit MVC View controller for corresponding variable = checkbox

question: if the sql database field type is BIT with 3 states, sql database type = bit (true/false/null)

1
  • What's the question here? Commented Jun 17, 2015 at 17:28

2 Answers 2

1

A checkbox only has 2 states... checked vs unchecked. So a third state for null wouldn't be logical. If you are trying to consider null as false, then do that conversion in your controller while creating your view model.

Something like myViewModel.MyBool = dto.MyBitColumn != null && dto.MyBitColumn;

Now you can use myViewModel.MyBool for your checkbox.

Sign up to request clarification or add additional context in comments.

3 Comments

If a 0 of unchecked and 1 is checked, what would the null be considering a checkbox?
If you go with the plan of making everything non nullable, you are going to run into more headaches because now you have to sanitize every field.
So if you are considering null as false (not selected), why not just make it a non nullable bool, which defaults to false? Now you do not need extra logic... or you can still do what I offered in my answer. I honestly think you are going to be angry at yourself when you start passing database poco's to your business logic and have to call .value everywhere as well as null reference checks.
0

The @CheckBoxFor() method only work for a boolean property. The signature of the method is

public static MvcHtmlString CheckBoxFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, bool>> expression)

Note the TResult in Func is bool. The reason that a checkbox has only 2 states - check or unchecked which equates to true or false.

If your property PType1 is bool? (nullable), you need to use

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

which will generate a dropdownlist with 3 values, "Not Set" (which equates to null), "True" and "False".

Side note: "observation: there is no checkbox control that can be used with a corresponding sql type of bit". Yes there is. @Html.CheckBox() can be use with bit so long as the field is NOT NULL which equates to bool. If you only want to store true or false then set the field to be NOT NULL

2 Comments

No its not possible, and I cant imagine why you think it could be. And you cannot bind to a string using a CheckBoxFor() - see my answer to your other question. What are you really trying to do? You cant save the value "YES" to a sql bit field!
You could create your own checkbox which will be grayed out if null so it can indicate never set. Once clicked on, it can turned un-grayed and unchecked to show the user set it explicitly to false. Then click again to check...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.