title: maxlength attribute of a text box from the DataAnnotations StringLength in Asp.Net MVC
body: <p>I am working on an MVC2 application and want to set the maxlength attributes of the text inputs.</p>
  
  <p>I have already defined the stringlength attribute on the Model object using data annotations and it is validating the length of entered strings correctly.  </p>
  
  <p>I do not want to repeat the same setting in my views by setting the max length attribute manually when the model already has the information.  Is there any way to do this?</p>
  
  <p>Code snippets below:</p>
  
  <p>From the Model:</p>
  
  <pre><code>[Required, StringLength(50)]
  public string Address1 { get; set; }
  </code></pre>
  
  <p>From the View:</p>
  
  <pre><code>&lt;%= Html.LabelFor(model =&gt; model.Address1) %&gt;
  &lt;%= Html.TextBoxFor(model =&gt; model.Address1, new { @class = "text long" })%&gt;
  &lt;%= Html.ValidationMessageFor(model =&gt; model.Address1) %&gt;
  </code></pre>
  
  <p>What I want to avoid doing is:</p>
  
  <pre><code>&lt;%= Html.TextBoxFor(model =&gt; model.Address1, new { @class = "text long", maxlength="50" })%&gt;
  </code></pre>
  
  <p>I want to get this output:</p>
  
  <pre><code>&lt;input type="text" name="Address1" maxlength="50" class="text long"/&gt;
  </code></pre>
  
  <p>Is there any way to do this?</p>