简体   繁体   中英

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?

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

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 (ie 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 , ie not null.

如果你使用c#6.0或更高版本,你可以使用:

public bool YourProp { get; set; } = false; 

This don't need initialisation. @Html.EditorFor(m => m.AnyNullableProperty) Below worked for me as expected.

        <div class="form-group">
            @Html.LabelFor(model => model.RequiresBatchNumberOnReceipt, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(m => m.RequiresBatchNumberOnReceipt)

                @Html.ValidationMessageFor(model => model.RequiresBatchNumberOnReceipt)
            </div>
        </div>

I've taken Chris Pratt idea but used it differently. I created a ViewModel and added a non-nullable property to update the nullable property and vice versa.

public bool? Public { get; set; }

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

Now in the View, you will use the non-nullable value for updating instead of the nullable value

Html.CheckBoxFor(model => model._Uploaded)

The only issue with this approach is that you will not get back null if saving changes. This worked for me as NULL value represent false in our program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM