title: ModelState.IsValid with DTO and Entity Framework code first
body: <p>ASP.NET 4.5, MVC 5, EF6 code first</p>
  
  <p>I'm a newbie and probably asking something long-known but I couldn't find solution on the web, probably because I don't know correct terminology to formulate this question.</p>
  
  <p>To simplify things, let's say I have two model classes Teacher and Kid; One kid can be assigned only to one teacher, but one teacher can have many kids. As I'm using code first, my database is constructed from these model classes:</p>
  
  <pre><code>public class Kid
  {
      [Required]
      public int Id { get; set; }
      [Required]
      public string Name { get; set; }
      [Required]
      public virtual Teacher { get; set; }    
  }
  
  public class Teacher
  {
      [Required]
      public int Id { get; set; }
      [Required]
      public string Name { get; set; }
      [Required]
      public virtual ICollection&lt;Kid&gt; Kids { get; set; }
  }
  </code></pre>
  
  <p>Now, I need to have a view for adding new kid with: 
  Textbox for Kid's name;
  Dropdown with list of Teachers</p>
  
  <p>So, I'm creating a data transfer object, specifically for that view:</p>
  
  <pre><code>public class AddNewKidViewDTO
  {
      public IEnumerable&lt;SelectListItem&gt; Teachers { get; set; }
      public int SelectedTeacherId { get; set; }
      public Kid Kid { get; set; }
  }
  </code></pre>
  
  <p>I also have a method for populating IEnumerable Teachers:</p>
  
  <pre><code>public AddNewKidViewDTO LoadTeachersForDropDownList()
  {
      ... //get the list of Teachers
      AddNewKidViewDTO addNewKidViewDTO = new AddNewKidViewDTO();
      List&lt;SelectListItem&gt; selectListItems =  new List&lt;SelectListItem&gt;();
      foreach (teacher in Teachers)
      {
          selectListItems.Add (new SelectListItem
          {
              Text = teacher.Name.ToString(),
              Value = teacher.Id.ToString()
          });
      }
      addNewKidViewDTO.Teachers = selectListItems;
      return addNewKidViewDTO;
  }
  </code></pre>
  
  <p>and in the view AddNewKid.cshtml</p>
  
  <pre><code>&lt;form&gt;
  @Html.LabelFor(model =&gt; model.Kid.Name)
  @Html.TextBoxFor(model =&gt; model.Kid.Name, new {id ="Name"}
  &lt;br/&gt;
  @Html.LabelFor(model =&gt; model.Kid.Teacher)
  @Html.DropDownListFor(model =&gt; model.SelectedTeacherId, Model.Teachers)
  &lt;/form&gt;
  </code></pre>
  
  <p>Form gets submitted and in the controller I get my populated AddNewKidViewDTO model:</p>
  
  <pre><code>[HttpPost]
  public ActionResult SaveNewKid (AddNewKidViewDTO addNewKidViewDTO)
  {
      if (ModelState.IsValid)
      {
          //here is where the problem comes
      }
  }
  </code></pre>
  
  <p>ModelState.IsValid in my case will always return false. 
  Because when it starts validating AddNewKidViewDTO.Kid, Teacher is compulsory field but in my addNewKidViewDTO model it's null. I have the necessary teacher Id contained in addNewKidViewDTO.SelectedTeacherId only.</p>
  
  <p>My question is, what is an elegant way to validate my model before passing to my inner business logic methods?</p>
  
  <p>Any help is appreciated.</p>