Yes, using the default HtmlHelpers
will achieve this for you
<%: Html.Checkbox("myCheckbox") %>
or with Razor
@Html.Checkbox("myCheckbox")
The Checkbox()
method will render a input type="hidden"
field along side the input type="checkbox"
that will submit the value false
when the checkbox is unchecked.
<input id="myCheckbox" name="myCheckbox" type="checkbox" value="true" />
<input name="myCheckbox" type="hidden" value="false" />
If you want to submit a value other than false
then you should render the checkbox and hidden field yourself setting the value of the hidden field to your default value. Note that they must have the same name attribute.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…