MVC将Html.CheckBoxFor与可为空的布尔一起使用 [英] MVC use Html.CheckBoxFor with nullable Bool

查看:155
本文介绍了MVC将Html.CheckBoxFor与可为空的布尔一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复选框要显示在我的视图中,该复选框与一个名为public的字段有关,该字段基本上说明特定的行是否为public.在数据库中,这是一个位字段,但由于以前使用的表的工作方式,因此它允许为空.

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.

我正在使用Html.CheckBoxFor,但它在抱怨这个字段,因为在系统中它不是布尔类型,而是布尔类型?类型.我想做的就是拥有它,以便如果该字段值是null,那么它在前端就被视为false(不幸的是,更新数据库值本身不是一种选择).

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).

我尝试使用GetValueOrDefault,并在模型文件中按照以下内容放置默认值:

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?

这是我试图用来显示复选框的视图上的代码.在这种情况下,我要添加一些额外的html属性,以使其显示为切换按钮而不是简单的复选框:

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.

关于这里的实际基本关注点,即将值设置为null(如果为null),则只需要一个自定义的getter和setter. @utaco的答案提供了一种新的,更简单的C#6.0自动实现的属性方法,具有默认值:

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;

对于早期版本的C#,您需要满足以下条件:

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

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

但是,当您不打算将Public设置为可为空的布尔值时,实际上却为null只会使您的代码更加困难.假设您可以将其更改为bool(即,这是一个视图模型,而不是绑定到数据库表的实际实体类),那么您应该这样做.不过,您仍然希望将private保留为可为空.这样一来,您可以在设置器中接受null,但可以将它们强制为getter中的false值,这意味着public的实际值将始终为truefalse,即不为null.

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.

这篇关于MVC将Html.CheckBoxFor与可为空的布尔一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆