I'm trying to render a radiobutton with some boolean attributes (required and disabled) using:
@Html.RadioButton("radio-name","false", new { id="test", required=Model.BooleanValue})
and
<input type="radio" name="radio-name" id="radio-name-no" value="false" class="radio" @if (!Model.BooleanValue) { <text>required</text> } disabled="@Model.BooleanValue" />
But the output is this:
<input id="test" name="radio-name" required="False" value="false" type="radio">
and
<input name="radio-name" id="radio-name-no" value="false" class="radio" disabled="True" type="radio">
MVC4 should render the boolean attributes as per HTML5 specs, so why it's outputting disabled="True"
(or False
) instead of disabled="disabled"
or disabled
(or nothing at all if the BooleanValue
property is false
)?
You can include conditionally rendered attributes like this:
<input
type="radio"
required="@(!Model.BooleanValue)"
disabled="@Model.BooleanValue" />
The solution was pretty strange. While doing some tests, I put an @if in the HTML input tag. This if was used to show the required
attribute. After this @if, I put the disabled="@Model.BooleanValue"
attribute.
It seems that this order disrupted the expected behaviour. Placing the disabled
attribute before the @if solved the problem (later I have deleted the @if and converted it to required="@(!Model.BooleanValue)"
).