I've got a checkbox that I want to display on my view related to a field called public, which basically says whether the particular row is public or not. In the database this is a bit field, but it allows nulls, due to how the table previously used to work.
I'm using Html.CheckBoxFor but it is complaining about this field because in the system it is not a bool type, but rather a bool? type. What I want to do is have it so that if the field value is null, then it counts as a false on the front end (unfortunately updating the database values themselves is not an option).
I have tried using the GetValueOrDefault, and putting a default value in my model file along the lines of:
public class Model
{
public bool? Public { get; set; }
public SearchModel()
{
Public = false;
}
}
however it was complaining about this, giving me the following error:
An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code
Additional information: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
So i'm not sure how I can progress from here, can someone help point me in the right direction?
EDIT:
This is the code on the view that i'm trying to use to show the checkbox. In this instance i'm adding some extra html attributes so that it appears as a toggle rather than a simple checkbox:
Html.CheckBoxFor(model => model.Public, new {data_toggle = "toggle", data_off = "No", data_on = "Yes", data_size = "small"})
Public, on an unrelated note your model is calledModelbut the constructor for it is calledSearchModelso that'll be causing some issues