Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello,

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

I am trying checkbox in my mvc application but its getting error.

@Html.CheckBoxFor(x => x.IsSelected) error in "IsSelected".

Please help me.

Thanks in Advance.

Ankit Agarwal
Software Engineer

What I have tried:

C#
[HttpPost]
        public string Index(IEnumerable<City> cities)
        {
            if (cities.Count(x => x.IsSelected) == 0)
            {
                return "You didn't select any city";
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("You selected -");
                foreach (City city in cities)
                {
                    if (city.IsSelected)
                    {
                        sb.Append(city.Name + ",");
                    }
                }
                sb.Remove(sb.ToString().LastIndexOf(","), 1);
                return sb.ToString();
            }
        }



@Html.CheckBoxFor(x => x.IsSelected)
Posted
Updated 15-Dec-16 17:02pm
Comments
F-ES Sitecore 15-Dec-16 4:19am    
If IsSelected is a model designed to be mapped to a checkbox then it should be defined as "bool" and not "bool?" as a checkbox only has two states, not three. If possible change the definition of IsSelected.

Do this:

C#
if (cities.Count(x => x.IsSelected == true) == 0)
 
Share this answer
 
Comments
Richard Deeming 15-Dec-16 12:43pm    
You're getting too far ahead - you've answered his next question! :D

And the one after that will probably be about the if (city.IsSelected) line.
#realJSOP 15-Dec-16 13:01pm    
I try to answer as many questions as possible with one answer.
Try with this

C#
@Html.CheckBoxFor(x => x.IsSelected.GetValueOrDefault())
 
Share this answer
 
v2
Comments
Richard Deeming 15-Dec-16 13:05pm    
This will result in an InvalidOperationException with the message "Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions".
If you can't change the model property from bool? to bool, then you're going to have to use the CheckBox and LabelFor methods, rather than the CheckBoxFor method:
@Html.CheckBox(nameof(Model.IsSelected), Model.IsSelected ?? false)
@Html.LabelFor(m => m.IsSelected)

Alternatively, you could use EditorFor with a custom editor, as described in this StackOverflow answer[^].

Or, you could add a non-nullable wrapper property to your viewmodel, and use that with the CheckBoxFor method:
C#
public bool IsSelectedNotNull
{
    get { return IsSelected ?? false; }
    set { IsSelected = value; }
}
@Html.CheckBoxFor(m => m.IsSelectedNotNull)
 
Share this answer
 
Usually, I would modify the view model and if the view model only purpose is to be a model for that view, you can always modify it.

Alternatively, if I would have many cases where I prefer to have a bool ? for a property and still have a check box, I would probably write an extension method that would handle that case.

Doing that however require to have a good understanding of C# as expression trees are somewhat unusual...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900