asp.net mvc - Cannot implicitly convert type 'bool?' to 'bool' Checkbox MVC -


i working checkboxes in mvc. have table 1 column bit type.the following code giving me error.

[httppost] public string index(ienumerable<city> cities)  {      if (cities.count(x => x.chosen) == 0)     {         return "you did not select city";      }      ...... } 

chosen bit type here. , when trying build says:

cannot implicitly convert type 'bool?' 'bool'. explicit conversion exists (are missing cast?)

error self explainary. x.chosen bool? type (nullable<bool>).

it mean should first check on null. example:

[httppost] public string index(ienumerable<city> cities)  {      if (cities.count(x => x.chosen.hasvalue && x.chosen.value) == 0)     {         return "you did not select city";      }      ...... } 

it's better write this:

[httppost] public string index(ienumerable<city> cities)  {      if (!cities.any(x => x.chosen.hasvalue && x.chosen.value))         return "you did not select city";      ...... } 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -