title: Conditional Attributes in Razor v2
body: <p>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.</p>
  
  <p>My HTML output looks like this...</p>
  
  <pre><code>&lt;label class="radio inline"&gt;&lt;input checked="true" id="ActivationType" name="ActivationType" type="radio" value="0"&gt;Option 0&lt;/label&gt;
  &lt;label class="radio inline"&gt;&lt;input checked="false" id="ActivationType" name="ActivationType" type="radio" value="1"&gt;Option 1&lt;/label&gt;
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>&lt;label class="radio inline"&gt;@Html.RadioButtonFor(x =&gt; x, "0", new{@checked = Model.HasValue &amp;&amp; (int)Model.Value == 0})Option 0&lt;/label&gt;
  &lt;label class="radio inline"&gt;@Html.RadioButtonFor(x =&gt; x, "1", new{@checked = Model.HasValue &amp;&amp; (int)Model.Value == 1})Option 1&lt;/label&gt;
  </code></pre>
  
  <p>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...</p>
  
  <pre><code>&lt;label class="radio inline"&gt;&lt;input checked="true" id="ActivationType" name="ActivationType" type="radio" value="0"&gt;Option 0&lt;/label&gt;
  &lt;label class="radio inline"&gt;&lt;input id="ActivationType" name="ActivationType" type="radio" value="1"&gt;Option 1&lt;/label&gt;
  </code></pre>
  
  <p>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</p>
  
  <pre><code>&lt;dependentAssembly&gt;
    &lt;assemblyIdentity name="System.Web.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral" /&gt;
    &lt;bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" /&gt;
  &lt;/dependentAssembly&gt;
  </code></pre>
  
  <p>Has anyone had any luck with this what-so-ever?</p>
  
  <p><strong>UPDATE:</strong> 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.</p>
  
  <p>This was an issue in MVC as noted here (<a href="https://stackoverflow.com/questions/6752013/mvc3-editortemplate-for-a-nullable-boolean-using-radiobuttons">MVC3 EditorTemplate for a nullable boolean using RadioButtons</a>) But I was hoping to get rid of the workaround now that it's purported fixed in Razor2/MVC4.</p>