Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

5 Answers

up vote 2 down vote accepted

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

share|improve this answer

You have to do it as follows:

<input type="checkbox" onclick = "InActiveUser('@item.UserId')" 
 id = "@item.UserId" checked="@item.IsActive" />
share|improve this answer
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. –  dombenoit Jun 12 at 20:28

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.

share|improve this answer

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')"/>
  }
}
share|improve this answer

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.

share|improve this answer
 
@user2433270 : Did you check this answer ? –  wuhcwdc Jun 14 at 2:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.