MVC 4 Razor中的帮助器Html CheckBoxFor helper——不能隐式地将类型'int'转换为'bool'

时间:2022-07-09 16:35:16

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'.

我正在尝试从一个外部db表中呈现复选框的列表,但我一直得到这个错误:不能将类型“int”转换为“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.

我猜我的强类型视图的b/c会返回一个列表。谁能请帮助。提前谢谢你。

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 个解决方案

#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?

你得给它点颜色看看。也许ischosen应该是bool,那是你想要的?

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 )
}

#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?

你得给它点颜色看看。也许ischosen应该是bool,那是你想要的?

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 )
}