简体   繁体   中英

Html.CheckBoxFor() checked problem in ASP.Net MVC 2

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?

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">

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM