title: ASP .NET MVC 3 Custom Html 5 Data- Attributes using C#
body: <p>I have the following Problem. </p>
  
  <p>I have Html elements that I want to affix Data- attributes and a specific class to Like so :</p>
  
  <pre><code>&lt;element id="" class="custom_class" data-attribute="value" data-attr-two="value2"&gt;&lt;/element&gt;
  </code></pre>
  
  <p>But I want To define these attributes at the model level like so :</p>
  
  <pre><code>public class ModelName 
  {
    [DataAnnotation({attribute = "value", attr_2 = "value2"})]
    [Class= "custom_class"]
    public type PropertyName { get; set;}
  }
  </code></pre>
  
  <p>I am Aware of Custom validation attributes but the problem With them is the element will look like this :  </p>
  
  <pre><code>&lt;element id="" class="custom_class" data-val-attribute="value"/&gt;&lt;/element&gt;
  </code></pre>
  
  <p>And not this :</p>
  
  <pre><code>&lt;element id="" class="custom_class" data-attribute="value"/&gt;&lt;/element&gt;
  </code></pre>
  
  <p>If there is another way to do this like Perhaps adding another dictionary to Html helpers Like this :</p>
  
  <pre><code>@Html.TextBoxFor(model =&gt; Model, new { DataAnnotation : DataAnnotationObjectModel.Create({Message =        "something", Name = "something" }) )
  </code></pre>
  
  <p>The DataAnnotationObject would be a class with all the types of data- attributes id be using and some methods wich create default objects that are pre enumerated with repetitive data.</p>
  
  <p>I Know I can do this :</p>
  
  <pre><code>@Html.TextBoxFor(model =&gt; Model, new { data_attr = @item.Value }  )
  </code></pre>
  
  <p>But Im going have to add 5 or more attributes and for every property in every model that I want to do it to and im going to have to do it atleast 3 times for each Property. </p>
  
  <p>Is there a way to do this? </p>
  
  <p>If you have some trouble Understanding this question please feel free to post a comment with your questions and I will try my best to explain</p>