title: Entity Framework - Validation (server + clientside, jquery) with data annotations, WITHOUT MVC?
body: <p>there are several tutorials that explain how to use EF data annotation for forms validation using the MVC framework. And to use jquery for the client side.</p>
  
  <p>See e.g.: <a href="http://dotnetaddict.dotnetdevelopersjournal.com/clientvalidation_mvc2.htm" rel="nofollow noreferrer">http://dotnetaddict.dotnetdevelopersjournal.com/clientvalidation_mvc2.htm</a></p>
  
  <p>I would like to achieve the same, but without using MVC/MVC2.</p>
  
  <p>I want to build a classic asp.net site, create the Entities Model, create my partial classes that include the validation (required, regex etc.).</p>
  
  <p>I created the entities model and the partial classes including the data annotations. I can add new records to the DB.</p>
  
  <p>What I miss is the validation. Now I can add records to the DB even if the fields are not valid, I would like to get the errors, and if possible  I would like to use jquery for the client validation (in MVC you just add <code>&lt;% Html.EnableClientValidation(); %&gt;</code> to the view...).</p>
  
  <p>Can you help me? Or point me to some good online resources that explain this?</p>
  
  <p>Thanks a lot.</p>
  
  <p><strong>EDIT</strong>: I found something here: </p>
  
  <p><a href="https://stackoverflow.com/questions/2493800/how-can-i-tell-the-data-annotations-validator-to-also-validate-complex-child-prop">How can I tell the Data Annotations validator to also validate complex child properties?</a></p>
  
  <p>I have an Entity called "User" and I created a partial class as follows:</p>
  
  <pre><code>using System.ComponentModel;
  using System.ComponentModel.DataAnnotations;
  
  namespace MySite.Models
  {
      [MetadataType(typeof(UserMetaData))]
      public partial class User
      {
      }
  
      public class UserMetaData
      {
          [Required(ErrorMessage = "Username Required")]
          [DisplayName("Username")]
          public string Username{ get; set; }
  
          [DisplayName("Password")]
          [Required(ErrorMessage = "Password Required")]
          [RegularExpression(@"^[^;&gt;;&amp;;&lt;;%?*!~'`;:,."";+=|]{6,10}$", ErrorMessage = "The password must be between 6-10 characters and contain 2 digits")]
          public string Password{ get; set; }
      }
  }
  </code></pre>
  
  <p>In the code behind of my page I've put a similar "isValid" check as in the above mentioned link:</p>
  
  <pre><code>var validationContext = new ValidationContext(person, null, null);
  var validationResults = new List&lt;ValidationResult&gt;();
  
  var isValid = Validator.TryValidateObject(userToAdd, validationContext, validationResults);
  
  if (isValid)
  {
  savetoDB();
  }
  </code></pre>
  
  <p>But when I debug... "isValid" is always "true", even if I leave the fields null. Help :-S</p>
  
  <p>EDIT2:</p>
  
  <p>It was always "true" because I was filling the "user" properties, as follows:</p>
  
  <pre><code>User userToAdd = new User();
  userToAdd.Username = txtUsername.Text;
  userToAdd.Password = txtPassword.Text;
  </code></pre>
  
  <p>I changed the object: from "User" to "UserMetaData" (<code>User userToAdd = new UserMetaData();</code>) then the validation works ("false" in case the regex is not respected) ... but still, quite weird... then I should create another "User" object and fill it again... not very clean...</p>