根据该值使复选框处于选中状态还是未选中状态?

问题描述:

我们如何基于该值以编程方式使复选框处于选中状态或未选中状态?也就是说,对于特定用户,如果该值为true,则应该选中该复选框;否则,如果该值为false,则需要取消选中该复选框.我通过以下方式声明了该复选框:

How can we make a checkbox checked or unchecked programatically based on the value? That is to say, for a particular user if the value is true, the checkbox should be checked, else if the value is false the checkbox needs to be unchecked. I declared the checkbox in the following way:

<input type="checkbox" class="checkbox">

if(condition = true)
{
@Html.CheckBoxFor(x => x.Test, new { @checked = "checked" })
}
else
{
@Html.CheckBoxFor(x => x.Test)
}

希望这会有所帮助:)