title: Skip all the validation of a model base on one of its property (ASP.Net Core 2.0)
body: <p>How I can skip all the validation of a model base on one of its property. For instance, if the <code>WillBeDeleted</code> property of the object is true the validator skips all the validation rules.</p>
  
  <pre><code>namespace ViewModel
  {
      public class ContactForm : IViewModel
      {
          [ValidateNever]
          public Contact Item { get; set; }
  
          public Category Category { get; set; }
  
          [Required, DisplayName("First name")]
          [StringLength(200, ErrorMessage = "First name should not exceed 200 characters.")]
          public string FirstName { get; set; }
  
          [Required, DisplayName("Last name")]
          [StringLength(200, ErrorMessage = "Last name should not exceed 200 characters.")]
          public string LastName { get; set; }
  
          public List&lt;TelephonesSubForm&gt; Telephones { get; set; } = new List&lt;TelephonesSubForm&gt;();
  
          public List&lt;EmailsSubForm&gt; Emails { get; set; } = new List&lt;EmailsSubForm&gt;();
  
          public class TelephonesSubForm : IViewModel
          {
              public int Id { get; set; }
  
              public bool MustBeDeleted { get; set; }
  
              [Required]
              public TelephoneAndEmailType Type { get; set; }
  
              [Required]
              [StringLength(200, MinimumLength = 10, ErrorMessage = "Telephone should not exceed 200 characters.")]
              public string Telephone { get; set; }
          }
  
          public class EmailsSubForm
          {
              public int Id { get; set; }
  
              public bool MustBeDeleted { get; set; }
  
              [Required]
              public TelephoneAndEmailType Type { get; set; }
  
              [Required]
              [StringLength(200, MinimumLength = 10, ErrorMessage = "Email should not exceed 200 characters.")]
              public string Email { get; set; }
          }
      }
  }
  </code></pre>
  
  <p>In the given sample model, when the <code>MustBeDeleted</code> is <code>true</code> it means that the item of email or telephone will be deleted on the save action.</p>
  
  <p>I have searched and found that several questions about conditional (custom) validation and they are suggesting to remove the specific keys from the <code>ModelState</code> before checking the validation state of it. Like <a href="https://stackoverflow.com/questions/2417113/asp-net-mvc-conditional-validation">this</a> one.</p>