1

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)?

8
  • Because your telling it to by using disabled="@Model.BooleanValue" - but what are you wanting to do here. disabled="true" and disabled="false" (or disabled="disabled" or just disabled) are all identical - its the presence of the attribute which determines if its disabled or not. You need to conditionally add the attribute. Commented Nov 15, 2017 at 9:21
  • Razor2 should, if the HTML5 attribute is a boolean attribute, automatically render it as disabled (or disabled=disabled) instead of disabled="True", and don't render it at all if the Model's property is false or null. It seems like in my case it does a ToString() and directly assigning the boolean's value to the attribute. Commented Nov 15, 2017 at 9:25
  • Not in the first case (by adding htmlAttributes using new { ... } (conditional attributes do not apply then). And the 2nd case does work and will generate disabled="disabled" if the property is true` and omit it if false - I can only assume your not using Razor2 Commented Nov 15, 2017 at 9:34
  • Thanks for the clarification for the first case.For the second one I've double checked the Razor dll version and it's 2.0.20710.0 Commented Nov 15, 2017 at 9:40
  • Works just fine for me in MVC-5 (cant test in MVC-4 at the moment) Commented Nov 15, 2017 at 9:44

1 Answer 1

3

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)").

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.