always v always v - 4 years ago 97
C# Question

Make a checkbox checked or unchecked based on the value?

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">

Answer Source

You can try this

<input type="checkbox" checked="@(required ? "checked" : "")" id="@inputCheckBoxId" name="@inputCheckBoxId"/>


(to keep thing simple, I have done what you see below)

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

Hope this helps :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download