Make a checkbox checked or unchecked based on the value?

Viewed 107881

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">
8 Answers
if(condition = true)
{
@Html.CheckBoxFor(x => x.Test, new { @checked = true })
}
else
{
@Html.CheckBoxFor(x => x.Test)
}

try this:

<input type="radio" name="filter_favorites" value="Favorites" @(loggedIn ? "checked" : "") />

So this is a simple radio button which checks loggedIn variable & if true radio button will be checked else not.

Related