3

I'm trying to get the conditional attributes support in Razor 2 to work, but for the life of me it's not behaving as expected.

My HTML output looks like this...

<label class="radio inline"><input checked="true" id="ActivationType" name="ActivationType" type="radio" value="0">Option 0</label>
<label class="radio inline"><input checked="false" id="ActivationType" name="ActivationType" type="radio" value="1">Option 1</label>

Now the problem with this HTML is that even I want Option 0 to be chcked, Option 1 is what's actually checked. Now this HTML was output by the following code in MVC, ans is sitting in a DisplayTemplate.

<label class="radio inline">@Html.RadioButtonFor(x => x, "0", new{@checked = Model.HasValue && (int)Model.Value == 0})Option 0</label>
<label class="radio inline">@Html.RadioButtonFor(x => x, "1", new{@checked = Model.HasValue && (int)Model.Value == 1})Option 1</label>

The problem is that the mere presence of the checked attribute is enough for the browser to consider it as checked regardless of it's actual value. So one of the features in Razor 2 (purportedly) is that if an attribute's value is null, an empty string or false that the attribute itself does not get rendered. What I expected to see is this...

<label class="radio inline"><input checked="true" id="ActivationType" name="ActivationType" type="radio" value="0">Option 0</label>
<label class="radio inline"><input id="ActivationType" name="ActivationType" type="radio" value="1">Option 1</label>

Now I've noticed lots of issues with this on StackOverflow so I'm obviously not the only person struggling with this problem. The thing is none of the questions seem to have a solid answer. I've even gone so far as to add an assembly redirect in my webconfig to ensure it's using the right version of Razor since I did migrate this app from MVC3 to MVC4

<dependentAssembly>
  <assemblyIdentity name="System.Web.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>

Has anyone had any luck with this what-so-ever?

UPDATE: I'm essentially trying to get a set of radio buttons that will let me programatically determine whether or not they are checked without having to define a code block where I create a new dictionary per checkbox to avoid rendering attributes that have no value.

This was an issue in MVC as noted here (MVC3 EditorTemplate for a nullable boolean using RadioButtons) But I was hoping to get rid of the workaround now that it's purported fixed in Razor2/MVC4.

Community
  • 1
  • 1
Nick Albrecht
  • 16,607
  • 10
  • 66
  • 101

1 Answers1

2

As of MVC 4, I have also found that the conditional attributes do not work in the html helper methods. If you put the conditional attribute directly in a html tag e.g.

<input checked="@(Model.HasValue && (int)Model.Value == 0)" id="ActivationType" name="ActivationType" type="radio" ...

you should find that it will work. I had a similar issue with passing the disabled attribute. Hope that helps.

Jason
  • 81
  • 1
  • 3