title: asp.net MVC: disable a TextBox
body: <p>I have a Html.TextBox() and I need to make it disabled in certain conditions. With the TextArea it goes like this:</p>
  
  <pre><code>&lt;%=primaryLang ? Html.TextArea("e.location", new { rows = 2, cols = 60 }) : Html.TextArea("e.location", new { rows = 2, cols = 60, disabled = "true" })%&gt;
  </code></pre>
  
  <p>But with TextBox it is not possible:</p>
  
  <pre><code>    &lt;%=primaryLang ? 
        Html.TextBox("e.startDate") : 
          Html.TextBox("e.startDate", new { disabled = "true"})%&gt;
  </code></pre>
  
  <p>It will issue {disabled=true} in the value. This is because the only function which will allow you to pass the HtmlAttributes will require also the model to be passed. This view is strongly typed, and the model is automatically filled in.</p>
  
  <p>If I pass it like this:</p>
  
  <pre><code>Html.TextBox("e.startDate", Model.e.startDate, new { disabled = "true"})
  </code></pre>
  
  <p>or like this:</p>
  
  <pre><code>Html.TextBox("e.startDate", null, new { disabled = "true"})
  </code></pre>
  
  <p>the GET version will work, but the POST version will issue a NullReferenceException. Both the above seem to have the exact same effect. Both will present the correct data from the model on GET.</p>
  
  <p>If I leave it lust like this:</p>
  
  <pre><code>Html.TextBox("e.startDate") 
  </code></pre>
  
  <p>it will work correctly, for both POST and GET...</p>
  
  <p>Why? Any ways to accomplish?</p>
  
  <p>Thanks! :)</p>
  
  <hr>
  
  <p>Thanks to the answers below, I solved it like this:</p>
  
  <pre><code>&lt;%=primaryLang ? 
          Html.TextBox("e.startDate") : 
            Html.Hidden("e.startDate") + Html.TextBox("e.startDate", null, new { disabled = "true"})%&gt;
  </code></pre>