Neethu
Neethu

Reputation: 85

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

I had delared :

public bool Valuetype { get; set; } in my model and in controller used Valuetype = s.IsValueType which is resulting Cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?) error .

Upvotes: 0

Views: 3323

Answers (2)

Rokive
Rokive

Reputation: 825

cast your nullable value to value type

public ActionResult Index(bool? checkOffAge)
{
    if (checkOffAge != null) {
       model.CheckOffAge =(bool)checkOffAge;
    }
}

Upvotes: 0

Mason Wheeler
Mason Wheeler

Reputation: 84540

Apparently s is a Nullable<bool>, also written as bool?. This is a value that can be a bool or it can be null. It's not valid to simply assume that it's a bool; you need to test to make sure it's not null first, and handle the null appropriately if it is.

If it doesn't make sense in this context for it to be a null at all, then try to fix the type of s to be a regular bool.

Upvotes: 1

Related Questions