0

I have dynamic check box in my view. But its not working fine its always getting checked here is my code:

<input type="checkbox" onclick = "InActiveUser('@item.UserId')" id = "@item.UserId"   checked="@(item.IsActive == false ? false : true)" />

Please help thanks.

user2433270
  • 5
  • 1
  • 2

5 Answers5

2

Checked attribute doesn't take true or false as a value. The sole presence of this attribute makes it checked. You have to remove this attribute if you want element to remain unchecked.

Read more in this answer

Community
  • 1
  • 1
Patryk Ziemkowski
  • 1,490
  • 12
  • 26
0

You have to do it as follows:

<input type="checkbox" onclick = "InActiveUser('@item.UserId')" 
 id = "@item.UserId" checked="@item.IsActive" />
Santosh Panda
  • 7,235
  • 8
  • 43
  • 56
  • 1
    Wrong. The checkbox will be checked if the `checked` attribute is present on the element, no matter what the value is. `checked="true"` and `checked="false"` behave the same. – dom Jun 12 '13 at 20:28
0

As Patryk said - you need to change the way the checked attribute is added. Something like this:

<input type="checkbox" onclick = "InActiveUser('@item.UserId')" id = "@item.UserId"   @(item.IsActive ? "checked" : "") />

Try that.

Richard Seal
  • 4,248
  • 14
  • 29
0

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.

wuhcwdc
  • 286
  • 1
  • 10
0

Try something like this:

 @{
  if (ViewBag.IsActive)
  {
    <input type="checkbox" id="@item.UserId" checked onclick = "InActiveUser('@item.UserId')"/>
  }
  else
  {
    <input type="checkbox" id="@item.UserId" onclick = "InActiveUser('@item.UserId')"/>
  }
}
Jitender Kumar
  • 2,439
  • 4
  • 29
  • 43