Hi I have some code like:
foreach (var item in Model) {
<div>
@Html.DisplayFor(modelItem => item.UserRole.Role.Name)
</div>
<div>
<input type="checkbox" name="ids" checked="@item.Active" />
</div>
}
What I was trying to achieve was set the checkbox check if item is Active else not checked.
I understand the syntax should be checked="checked" and I’ve set it up as checked="true"
I don’t understand how to convert the true to checked and what to do for false.
I was trying to use a ternary but it wouldn’t play nice. Can anybody help?
The presence of the checked attribute will cause it to be checked, i.e It doesn’t matter (AFAIK) what the value of the attribute is. So you need to detect whether or not to add the checked attribute at all:
Edited: changed to MДΓΓ БДLL version, much cleaner.
One brute force approach =P