0

I have several checkboxes on a form. Only one of them is checked when it needs to be; all the others are unchecked regardless of whether the isChecked parameter is passed in as either true or false.

The checkboxes are coded like this:

    <%= Html.CheckBox("cbName",Model.checkvalue)%>
    <%= Html.CheckBox("cbName1",Model.checkvalue1)%>

I have stepped through the code and Model.checkValue and Model.checkValue1 are both true, but cbName is not checked and cbName1 is checked (in fact, in my actual app' there are several more CheckBoxes and none are checked -except the second one in the form- although the Model properties are all true in the test I ran).

Has anyone come across this (mis)behavior before & can you let me know where I am going wrong, please? I can't find a similar question anywhere, so I am hoping I am just making a simple error that will be quick to fix...

tereško
  • 58,060
  • 25
  • 98
  • 150
lukkea
  • 3,606
  • 2
  • 37
  • 51
  • This article might lend some insight: http://stackoverflow.com/questions/1127357/why-does-asp-net-mvc-html-checkbox-output-two-inputs-with-the-same-name – Joel Etherton Sep 01 '11 at 11:56
  • thanks Joel; that article is about submission - the problem I have is setting up the form in the first place. I think the two issues are separate. I have just found out about CheckBoxFor and am going to give it a try; I will post back here my results... – lukkea Sep 01 '11 at 12:27
  • Which is why I didn't submit it as an answer. I was hoping perhaps something relative in the article might lead you to the true solution. – Joel Etherton Sep 01 '11 at 13:25
  • Please show the controller action that is rendering this view. – Darin Dimitrov Sep 01 '11 at 20:31

3 Answers3

1

what about use different way of rendering the code ( of course you can later simplify this code or create your own helper):

<%
if(Model.checkvalue1){
%>
<%= Html.CheckBox("name", new {checked =checked }) %>
<%}else{%>
<%= Html.CheckBox("name", null) %>
<%}%>

idea 2 make sure that the value you are passing in is boolean: therefore cast is as boolean

 <%= Html.CheckBox("cbName1",(bool)Model.checkvalue1)%>

idea 3 before using the code

<% bool myTempValue = Model.checkvalue1; %>
<%= Html.CheckBox("cbName1",myTempValue)%>
cpoDesign
  • 8,953
  • 13
  • 62
  • 106
0

There could be some reasons for this behavior:

Is there any query string parameter named cbName in URL of the page? Or, is it a POST request of the form? Query string and form (POST) data take precedence over explicit values set in code.

What browser are you using? FireFox sometimes preserves form data over page reloads. If you check a checkbox and refresh the page, FireFox checks the checkbox again, even when there is no "checked" attribute in HTML input element.

dkl
  • 3,850
  • 2
  • 27
  • 26
0

It was because the underlying data uses a nullable boolean.

I switched the CheckBox to a CheckBoxFor and got the error outlined in this question and this told me that the problem is the fact that the underlying data currently uses a nullable boolean. Since this data type will be switched for a not null boolean once ready I don't need to work around this.

I hope this answer helps someone else.

Thank you for everyone's contributions.

Community
  • 1
  • 1
lukkea
  • 3,606
  • 2
  • 37
  • 51