不能隐式转换类型'bool?到“bool”复选框MVC。

时间:2022-02-05 16:35:01

I am working with checkboxes in MVC. I have a table with one column as bit type.The following code giving me an error.

我正在使用MVC中的复选框。我有一个表,其中一列是位类型。下面的代码给了我一个错误。

[HttpPost]
public string Index(IEnumerable<City> cities) 
{ 
    if (cities.Count(x => x.Chosen) == 0)
    {
        return "You did not select any city"; 
    }

    ......
}

Chosen is a bit type here. and when I am trying to build it says:

这里选择是位类型。当我试图建立它的时候

Cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?)

不能隐式转换类型'bool?”“bool”。存在显式转换(您是否丢失了强制转换?)

2 个解决方案

#1


1  

Error is self explainary. Your x.Chosen is bool? type (Nullable<bool>).

错误是自我explainary。你的x。选择保龄球吗?类型(Nullable < bool >)。

It mean you should first check it on null. like this for example:

这意味着你应该首先检查它是否为空。这样的例子:

[HttpPost]
public string Index(IEnumerable<City> cities) 
{ 
    if (cities.Count(x => x.Chosen.HasValue && x.Chosen.Value) == 0)
    {
        return "You did not select any city"; 
    }

    ......
}

It's even better to write like this:

最好这样写:

[HttpPost]
public string Index(IEnumerable<City> cities) 
{ 
    if (!cities.Any(x => x.Chosen.HasValue && x.Chosen.Value))
        return "You did not select any city"; 
    ......
}

#2


0  

It occurs because the field Chosen is nullable in your database & it is non nullable in your model. To overcome this

之所以会出现这种情况,是因为选择的字段在数据库中是可空的,而在模型中是不可空的。为了克服这个

[HttpPost]
public string Index(IEnumerable<City> cities) 
{ 
    if (cities.Count(x => x.Chosen.Value) == 0)
    {
        return "You did not select any city"; 
    }
}

or else change the field Chosen in your model as nullable. For Ex.

或者将模型中选择的字段更改为nullable。前女友。

public bool? Chosen { get; set; }

then you can simply use

然后你可以简单地使用

if (cities.Count(x => x.Chosen) == 0)

#1


1  

Error is self explainary. Your x.Chosen is bool? type (Nullable<bool>).

错误是自我explainary。你的x。选择保龄球吗?类型(Nullable < bool >)。

It mean you should first check it on null. like this for example:

这意味着你应该首先检查它是否为空。这样的例子:

[HttpPost]
public string Index(IEnumerable<City> cities) 
{ 
    if (cities.Count(x => x.Chosen.HasValue && x.Chosen.Value) == 0)
    {
        return "You did not select any city"; 
    }

    ......
}

It's even better to write like this:

最好这样写:

[HttpPost]
public string Index(IEnumerable<City> cities) 
{ 
    if (!cities.Any(x => x.Chosen.HasValue && x.Chosen.Value))
        return "You did not select any city"; 
    ......
}

#2


0  

It occurs because the field Chosen is nullable in your database & it is non nullable in your model. To overcome this

之所以会出现这种情况,是因为选择的字段在数据库中是可空的,而在模型中是不可空的。为了克服这个

[HttpPost]
public string Index(IEnumerable<City> cities) 
{ 
    if (cities.Count(x => x.Chosen.Value) == 0)
    {
        return "You did not select any city"; 
    }
}

or else change the field Chosen in your model as nullable. For Ex.

或者将模型中选择的字段更改为nullable。前女友。

public bool? Chosen { get; set; }

then you can simply use

然后你可以简单地使用

if (cities.Count(x => x.Chosen) == 0)