Set checked property using comparison operator (MVC)

I'm looking for a way to set a checked property based on an integer (not boolean in this case) inside an MVC view.

Can this be expressed only within a view? (with our no html helper good / me)

0


a source to share


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


<%= Html.Checkbox("CheckboxName", someInt != 0) %>

      



+1


a source







All Articles