I need a checkbox but the underlying data is of type smallint in the database. Not sure how to make the @Html.Checkbox with that datatype. It complains saying the following:
Cannot implicitly convert type ‘short?’ to ‘bool’
Here is the code that I have:
@Html.CheckBoxFor(model => model.HasCycle)
If you are storing a boolean value in the database, then you should use the DB type ‘bit’ instead of smallint (where 0 will be false and 1 will be true).
Otherwise, you will need to first convert model.HasCycle to a bool. Also, since it is of type short? (nullable), you will need to handle null values too. You will probably want to handle this in the model itself, and publicly expose HasCycle from the model as a bool instead of a short. Still, you may run into some problems going back and forth, and the right way to do it is to change the database type.
To convert from a short? to a bool you can do something like: