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 开发者_JS百科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
精彩评论