title: How to hide a Model class field based on custom logic in MVC Web Api RC
body: <p>I am using Asp.Net Mvc Web api RC. </p>
  
  <p>I wanted to hide the fields/properties of my model class using custom attribute. Below is my class:</p>
  
  <pre><code>public class Employee
  {              
       public int EmpId { get; set; }       
       public string Name{ get; set; }
  
       //Wanted to hide this attribute based on custom logic. Like for a certain role, i want to hide the designation
       public string Designation{ get; set; }
  
       public string Department{ get; set; }
  }
  </code></pre>
  
  <p>How can we achieve using Data Annotations. I mean i wanted to create a separate attribute to use in this manner:</p>
  
  <pre><code>[HideForRoles(Roles="Admin,Writer")]
  public string Designation{ get; set; }
  </code></pre>
  
  <p><strong>UPDATE :</strong></p>
  
  <p>As i am developing web api. The response is serialized to either XML or Json format depend upon the formatter. So better question would be how not to allow the fields to be serialize while writing to the response. </p>
  
  <p>However one option could be using IgnoreDataMember attribute. Like</p>
  
  <pre><code>[IgnoreDataMember]
  public string Designation{ get; set; }
  </code></pre>
  
  <p>But the above is a compile time declaration where i cannot impose any condition. </p>
  
  <p>Question: How to ignore the field/property while serializing based on some condition at runtime?</p>