MVC3 HTML.CheckBoxFor error

3.1k Views Asked by At

I have a C# MVC3 project and am having trouble implementing the HTML.CheckBoxFor. I am getting the below error

Cannot implicitly convert type 'string' to 'bool'

Here's the code:

@{ 

List<Domain.LookupCostReductions> costReductions = ViewBag.CostReductions;
    foreach (Domain.LookupCostReductions cr in costReductions)
    { 
        @: <td style="border:0 ;vertical-align: top; ">  
        @Html.CheckBoxFor(x => x.CostReduction, cr.Description) 
        @cr.Description 
        @:</td> 
    } 
}

Any ideas?

2

There are 2 best solutions below

0
On BEST ANSWER

Rather than convert on the view, I would recommend your view model have the boolean data type for the CostReduction property.

0
On

x.CostReductionField should be of type bool.

Try this

  @Html.CheckBoxFor(x => Convert.ToBoolean(x.CostReduction), cr.Description)