ASP.NET Core MVC Checkbox for int values

Steve Ellwood
1 min readJun 29, 2023

I have a legacy system that is being updated to .NET Core 6 with a SQL Server database. One of the fields is an integer datatype but the data it holds is used as a boolean. In my case, a value of 1 corresponds to True and a 0 corresponds to false.

I wanted this to display as a checkbox. This is one of those things that is easy when you know how to do it. What worked for me was firstly to decide that a 1 corresponds to a True value and anything else corresponds to a false value, this covered any stray values that might appear in the database. This also makes it much simpler to convert the integer to a Boolean and back.

On pages where the value could be edited, I simply used the following

<input class="form-check" type="checkbox" @(Model.MyIntegerField==1 ? "checked": "") />

If on the other hand I wanted a items where the checkbox would be read e.g. in a list or when showing details only then I used the following

bool isChecked = item.MyIntegerField == 1;
@Html.CheckBoxFor(modelItem => isChecked, new {@disabled =true})

This disables the checkbox as you can see which gave me the result I wanted.



Steve Ellwood

Senior Integrations Officer at Doncaster Council Any views expressed are entirely my own.