1

It doesn't seem that the Html.CheckBoxFor helper adds the correct "checked" attribute when rendering the HTML.

I have a bool property rendered like so:

<%= Html.CheckBoxFor(m => m.Visible) %>

And the outputted HTML is this:

<input type="checkbox" value="true" name="Visible" id="Visible">

Is there some particular reason it does not add the "checked" attribute when the value is true?

2 Answers 2

4

This was a silly problem. I had forgot to add the bindings for my new Visible field and had only added it to my POCO class, therefore it was always false. Also, the value of the input tag was a red herring as it is always set to true, the actual value comes from a hidden field rendered right beneath the input tag like so:

<input type="hidden" value="false" name="Visible">
0
0

I tried this solution :

      <%if (Model.Enabled == true)
                             { %>
                           <input id="Enabled" checked="checked" 
                           type="checkbox" name="Enabled" 
                           value ="True" />
                           <%}
                             else
                             { %>
                            <input id="Enabled" 
                           type="checkbox" name="Enabled" 
                           value ="False" />
                           <%} %>

Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.