views:

424

answers:

4

Why is the checkbox not checked with this code?

<td><%=Html.CheckBox("ItemIsActive", item.ItemIsActive)%></td>

Item.ItemIsActive is of type boolean and set to true?

When i do this, it shows "true" in the view

<td><%=item.ItemIsActive%></td>
A: 

I've played with this for around 30 minutes now and I have tried the following;

1) Create a checkbox and set to true 2) Create a variable, set to true and use it as the checked flag 3) Create two checkboxes with the same id setting one to false the other to true

All of the above works fine so the (only) thing I can think of is that you may have some css or javascript setting checkboxes back to false. This is easy to do if you are using jQuery.

The fact that there are so few replies to this question seems to indicate that others can't seem to replicate your issue either.

Like I said, I'd be checking my CSS and any Javascript/jQuery I have on the page, partial controls, master etc.

griegs
Hi, thx for your time.I tried this:<td><%=item.ItemIsActive%></td><td><%=Html.CheckBox("ItemIsActive", item.ItemIsActive)%></td>As you see i'm using item.ItemIsActive in the same view. When i run the site this is what i see:<td>False</td><td><input id="ItemIsActive" name="ItemIsActive" type="checkbox" value="true" /></td>Is something wrong with the way i set up the html.checkbox?
kevinius
A: 

If you have a viewdata item called ItemIsActive, or if your view is tied to a specific model that has a property called that, it will use that over what you specify.

To see if this might be it try changing it to this:

<td><%=Html.CheckBox("ItemIsActive_test", item.ItemIsActive)%></td>

If that works then I would bet something in the viewdata, or in the model is overwriting what item.ItemIsActive contains

Solmead
item.ItemIsActive contains a boolean type coming from a database record:Dim Connection = DB.MijnLijstjeItems.ToList()Return Connection
kevinius
@kevinus, you might want to try @solmead advice and have a test that sets a variable called say 'slapy' to true and use that within your checkbox to see if that works. you may actually have a conflict.
griegs
Or even just do:<td><%=Html.CheckBox("ItemIsActive", true)%><%=Html.CheckBox("ItemIsActive_Test", true)%></td>
Solmead
To see if there is a conflict
Solmead
+1  A: 

I had this same issue until I changed my boolean field to a non-nullable field. Make sure your Item.IsActive field is declared like:

bool ItemIsActive {..}

instead of

bool? ItemIsActive {..}

If you are using Linq2Sql make sure ItemIsActive is not nullable in your table.

dr
A: 

I had the same problem as well (using ASP.NET MVC).

Actually I had a form with two checkboxes, one where the boolean is nullable and the other non-nullable.

In the case of the non-nullable the checkbox was checked when the value from the database was true. In the case of the nullable I couldn't get it checked using Html.CheckBox.

I solved it by converting the value from the model to non-nullable boolean.

<td><%=Html.CheckBox("ItemIsActive", item.ItemIsActive == null ? 
  false : 
  (bool)item.ItemIsActive) %></td>
Ego Centric