mvc html.checkboxfor 不是bool类型,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"})

回答1:

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.

回答2:

if you use c# 6.0 or higher you can use this:

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

回答3:

This don't need initialisation. @Html.EditorFor(m => m.AnyNullableProperty)

Below worked for me as expected.

@Html.LabelFor(model => model.RequiresBatchNumberOnReceipt, htmlAttributes: new { @class = "control-label col-md-2" })

@Html.EditorFor(m => m.RequiresBatchNumberOnReceipt)

@Html.ValidationMessageFor(model => model.RequiresBatchNumberOnReceipt)

回答4:

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.

来源:https://stackoverflow.com/questions/40490036/mvc-use-html-checkboxfor-with-nullable-bool

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值