2

I am trying to render list of checkboxes from an external db table BUT I keep getting this error: Cannot implicitely convert type 'int' to 'bool'.

I am guessing its not happy b/c of my strongly type view which returns a list. Can anyone please help. Thank you in advance.

my model

public partial class tblCity
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int IsSelected { get; set; }
} 

my view

@model List<Demo.Models.Sample>    

@for (int i = 0; i < Model.Count; i++)
{    
   @Html.CheckBoxFor(m => m[i].ID)  **Cannot implicitly convert type 'int' to 'bool'**
}

1 Answer 1

1

This is because you are giving it an int

@for (int i = 0; i < Model.Count; i++)
{    
   @Html.CheckBoxFor(m => m[i].ID) <- ID is an Int
}

You'd need to give it a bool. Maybe IsSelected was supposed to be a bool, and that was what you were looking for?

public partial class tblCity
{
    public int ID { get; set; }
    public string Name { get; set; }
    public bool IsSelected { get; set; }
} 

Then the view

@model List<Demo.Models.Sample>    

@for (int i = 0; i < Model.Count; i++)
{    
   @Html.CheckBoxFor(m => m[i].IsSelected )
}
Sign up to request clarification or add additional context in comments.

2 Comments

Here is the thing. The 'tblCity' model was generated from a third party database/table and I don't have control over the field data-type and the table doesn't not have any field that returns a bool. For a @tml.CheckBoxFor() to work does it have to have a bool field or can I simply replace public int IsSelected { get; set; } to public bool IsSelected { get; set; } on my generated model?
CheckBoxFor has to take a boolean, so if you change the model, that should be fine. As long as the object going in is a boolean.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.