James Peel James Peel - 1 year ago 498
C# Question

MVC use Html.CheckBoxFor with nullable Bool

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?


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"})

Answer Source

The specific exception you're getting occurs when you pass an expression to one of the templated helpers that can't be evaluated. Bear in mind that when you're using the expression-based helpers, you're not actually passing a property by value but rather an expression that represents a property on your model and which the helper will use to reference that property, generate field names from, etc.

You haven't shown the actual code where you're doing this, but this means essentially you can't do something like:

@Html.EditorFor(m => m.Public.GetValueOrDefault())

Because the templated helper cannot resolve that as an expression that matches up with a property on your model.

As to your actual base concern here, namely setting the value to false if it's null, you just need a custom getter and setter. @utaco's answer provides the new easier C# 6.0 method of auto-implemented properties with defaults:

public bool? Public { get; set; } = false;

For previous versions of C#, you need the following:

private bool? public;
public bool? Public
    get { return public ?? false; }
    set { public = value; }

However, keeping Public as a nullable bool when you have no intention of it ever actually being null just makes your code more difficult. Assuming you can change that to just bool (i.e. this is a view model and not the actual entity class tied to your database table), then you should do so. You still want to keep the private as a nullable though. That allows you accept nulls in the setter but coerce them into false values in the getter, meaning the actual value of public will always be either true or false, i.e. not null.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download