title: How to assign and use variable in MVC Razor View
body: <p>I've identified several of my views that require some simple logic, for example to add a class to a set of controls created with @Html helpers. I've tried several different ways, but they either throw errors in the View or just don't work. </p>
  
  <p><strong>A simple example:</strong></p>
  
  <p>Assign variable:</p>
  
  <pre><code>@if( condition )
  {
      var _disabled = "disabled";
  }
  
  @Html.CheckBoxFor(m =&gt; m.Test, new { @class = "form-control " + @_disabled })
  </code></pre>
  
  <p>Or:</p>
  
  <pre><code>@if( condition )
  {
      var _checked = "checked";
  }
  
  @Html.CheckBoxFor(m =&gt; m.Test, new { @checked = @_checked })
  </code></pre>
  
  <p>Of course, these doesn't work. I'm just trying to eliminate a bunch of <code>@if</code> conditions in my Views, but I have other lightweight logic uses for using variables. My problem might be more of how to use a variable in this way than actually assigning it?</p>