title: MVC 3: Conditionally Adding the Disabled Attribute with the HtmlHelpers
body: <p>I have an ASP.Net MVC 3 web application and I am adding a check box to a view page using the HtmlHelper class, like this...</p>
  
  <pre><code>@Html.CheckBox("CheckBox1", true, new { @class = "Class1" })
  </code></pre>
  
  <p>What I want to do is conditionally add the disabled attribute based on a view state property. Basically the following would be ideal...</p>
  
  <pre><code>@Html.CheckBox("CheckBox1", true, new { @class = "Class1", @disabled = Model.ReadOnly })
  </code></pre>
  
  <p>Unfortunately, due to the nature of the disabled attribute, this will not work because <strong><em>any</em></strong> value assigned to the disabled attribute (even "false") will be translated as true.</p>
  
  <p>I have already thought of a few solutions to get round this problem, so the question is not how can I do this. But rather, is there a simple way like the desired method above? or do I have to resort to one of the following?..</p>
  
  <p>What I know I could do...</p>
  
  <ol>
  <li><p>Create an if/else statement and write to different <code>Html.CheckBox</code> lines (not great for readability - and possible with throw a mark-up warning - not sure)</p></li>
  <li><p>Skip the HtmlHelper class and hand write the tag allowing for better conditionally attributes (keeps the code shorter, but adds inconsistency)</p></li>
  <li><p>Create a custom helper that takes a "disabled" parameter (the cleanest solution, but requires undesired extra methods - probably the best option so far though)</p></li>
  </ol>