title: Unobtrusive Validation on PostBack Created Class
body: <p>I'm having a little trouble making my request form people proof, so it can only submit valid information and tell the user what is wrong if not.</p>
  
  <p>I want the new properties (a new contact's) created by the controller to be validated as false by unobtrusive JS when the form posts back.</p>
  
  <p><strong>Important -</strong>
  <em>This is using <code>BeginCollectionItem</code> to add and remove <code>Contact</code> objects from the property <code>cmp.contacts (List&lt;Contact&gt;)</code> dynamically.</em></p>
  
  <p>These properties are required, but can be removed from the form. When submitted, the controller checks for them, if not there, it re-adds them to the form.</p>
  
  <p>Due to the properties being just added, they're technically not invalid.</p>
  
  <p><strong>Update - further investigation</strong></p>
  
  <p><em>However, this does seem strange, as I have made "" as the string values invalid due to requiring a character length of 3 at minimum, set in the model. So in the controller at post back, I check if the list is null, which it is (if the user has deleted it) and create a new list, with a new contact, which then goes through the <code>ModelState.IsValid</code> check, which it passes. The contact's properties are string values of "" which should be invalid, but it isn't marking them as invalid until the second post back, why is this?</em></p>
  
  <p><strong>Example story:</strong></p>
  
  <p>User removes the contact list from the page (you can have 1+ contacts), submits the form, the form fails because it needs at least a contact. The controller creates a new contact and posts the form back to the form. There are contact properties displayed on the page, but because they're newly created, they're not invalid.</p>
  
  <p>If the user tries to submit them again, they're marked as invalid, however I want it to be glaringly obvious what the user needs to fill out.</p>
  
  <p>Can I set them as invalid on post back?</p>
  
  <p><strong>Partial View with the problem fields:</strong></p>
  
  <pre><code>@model Contact
  &lt;div class="editorRow"&gt;
      @using (HtmlHelpers.BeginCollectionItem.HtmlPrefixScopeExtensions.BeginCollectionItem(Html, "contacts"))
      {
          &lt;div class="ui-grid-c ui-responsive"&gt;
              &lt;div class="ui-block-a"&gt;
                  &lt;span&gt;
                      @Html.TextBoxFor(m =&gt; m.name)
                  &lt;/span&gt;
              &lt;/div&gt;
              &lt;div class="ui-block-b"&gt;
                  &lt;span&gt;
                      @Html.TextBoxFor(m =&gt; m.telephone)
                  &lt;/span&gt;
              &lt;/div&gt;
              &lt;div class="ui-block-c"&gt;
                  &lt;span&gt;
                      @Html.TextBoxFor(m =&gt; m.email)
                  &lt;/span&gt;
              &lt;/div&gt;
              &lt;div class="ui-block-d"&gt;
                  &lt;span&gt;
                      @Html.DropDownListFor(m =&gt; m.type, new List&lt;SelectListItem&gt;
                 {
                     new SelectListItem { Text = "Admin", Value = "Admin" },
                     new SelectListItem { Text = "Peer", Value = "Peer" },
                     new SelectListItem { Text = "Technical", Value = "Technical" }
                 })
              &lt;span class="dltBtn"&gt;
                  &lt;a href="#" class="deleteRow"&gt;
                      &lt;img src="~/Images/DeleteRed.png" style="width: 15px; height: 15px;" /&gt;
                  &lt;/a&gt;
              &lt;/span&gt;
          &lt;/span&gt;
      &lt;/div&gt;
  &lt;/div&gt;
      }
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public ActionResult Create()
          {
              var cmp = new Company
              {
                  contacts = new List&lt;Contact&gt;
                  {
                      new Contact { email = "", name = "", telephone = "", type = "" }
                  }
              };
              return View(cmp);
          }
  
  [ValidateAntiForgeryToken]
          public ActionResult Create(Company cmp)
          {
              if (ModelState.IsValid)
              {
                  db.companys.Add(cmp);
                  db.SaveChanges();
                  EmailSupport.SendEmail(cmp, "Request Form");
                  return RedirectToAction("Thankyou", "Home");
              }
  
              if (cmp.contacts == null)
                  cmp.contacts = new List&lt;Contact&gt;
                  {
                      new Contact { email = "", name = "", telephone = "" } // this row of properties to show required
                  };
  
              return View(cmp);
          }
  </code></pre>
  
  <p><strong>Model</strong></p>
  
  <pre><code>[Table("Company")]
      public class Company
      {
          [Key]
          public int companyId { get; set; }
          [Required(ErrorMessage="Company name required.")]
          public string name { get; set; }
          [Required(ErrorMessage = "Telephone required.")]
          public string telephone { get; set; }
          [Required(ErrorMessage="Registration Number required.")]
          public string regNumber { get; set; }
          [EnsureOneItem]
          public List&lt;Contact&gt; contacts { get; set; }
      }
      [Table("Contact")]
      public class Contact
      {
          [Key]
          public int contactId { get; set; }
          public int companyId { get; set; }
          [ForeignKey("companyId")]
          public Company company { get; set; }
          [Required(ErrorMessage="Contact name required.")]
          public string name { get; set; }
          [Required(ErrorMessage="Telephone required.")]
          public string telephone { get; set; }
          [Required]
          [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                              @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                              @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$",
                              ErrorMessage = "Email is not valid.")]
          public string email { get; set; }
          [Required(ErrorMessage = "Contact type required.")]
          public string type { get; set; }
      }
  </code></pre>