Approach - 1
@if(item.IsActive)
{
<input type="checkbox" onclick = "InActiveUser('@item.UserId')"
id = "@item.UserId" checked="checked" />
}
else
{
<input type="checkbox" onclick = "InActiveUser('@item.UserId')"
id = "@item.UserId"/>
}
Approach - 2
You can use below code.
$("input[type='checkbox']").prop('checked', true); // Checked
$("input[type='checkbox']").prop('checked', false); // Un-Checked
If you pay attention to the above code, prop function is used to set the attribute value. You can use JQuery - 1.6 version. Now based upon your condition, you can write Razor code in the JavaScript section like below..
<script type="text/javascript">
$(document).ready(function () { //Some Razor code in JQuery
var status = "@status" == "True" ? true : false;
$("input[type='checkbox']").prop('checked', status);
});
</script>
Server side sample code in View.cshtml
@{
var status = false;
}
Approach - 3
JQuery
<script type="text/javascript">
$(document).ready(function () { //Some Razor code in JQuery
var status = "@status" == "True" ? true : false;
if (!status)
$("input[type='checkbox']").removeAttr('checked');
else
$("input[type='checkbox']").attr('checked', true);
});
</script>
Server side code in cshtml
@{
var status = true;
}
If you pay attention to the above JQuery code, I am removing the checked attribute using the removeAttr
in case status value is false and setting it to true using the attr
function when status is true.