Set checked property using comparison operator (MVC)
2 answers
You can use standard sentences if
and such directly in the view:
<% if (myInt > 3) { %>
<input name="checkbox1" type="checkbox" checked="checked">a checked box</input>
<% } else { %>
<input name="checkbox1" type="checkbox">a non-checked box</input>
<% } %>
Of course the Craig version will be much better in your code ...;)
<%= Html.CheckBox("checkbox1", myInt > 3) %>
<label for="checkbox1">a box that might be checked...</label>
Note that you need a label tag to get the title for your checkbox - the html helper doesn't give you that for free. Unless, of course, you are using one of the overloads that take IDictionary
html attributes for an argument ...
+2
a source to share