title: C#, Valid ModelState fails .isValid
body: <p>C# valid ModelState fails .isValid   </p>
  
  <h2>Search Controller</h2>
  
  <pre><code>//
  // GET: /Search/Create
  public ActionResult Create()  { return View();  } 
  
  //
  // POST: /Search/Create
  [HttpPost]
  public ActionResult Create(Search search)
  {
      search.Created = DateTime.Now;
  
      search.SearchSet = "test data";
      search.URLParameter = 1432567389;
  
      if (ModelState.IsValid)
      {
          _db.Searchs.Add(search);
          _db.SaveChanges();
          return RedirectToAction("Index");
      }
  
      return View(search); 
  }
  </code></pre>
  
  <h2>Search Class</h2>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.ComponentModel.DataAnnotations;
  
  namespace TalentProfile.Models
  {
      public class Search
      {
          public int Id { get; set; }
          [Required]
          public int URLParameter { get; set; }
          [Required, MaxLength(50, ErrorMessage = "Client must be 50 characters or less")]
          public string Client { get; set; }
          [DataType(DataType.MultilineText), StringLength(150, ErrorMessage = "{0} must be {1} characters or less")]
          public string Notes { get; set; }
          [Required]
          public string SearchSet { get; set; }
          [Required]
          public DateTime Created { get; set; }
      }
  }
  </code></pre>
  
  <p>If I run in debug all the fields in the class are properly set but it fails <code>ModelState.IsValid</code>. If I drill into the <code>ModelState.IsValid</code> it is false. Drilling down further I find the error “The SearchSet field is required”.
  The <code>SearchSet</code> field is properly set to “test data”. If I remove the <code>ModelState.IsValid</code> check the save to the database succeeds.</p>
  
  <p>Why am I getting the “field is required” error if the field contains valid data.</p>
  
  <h2>Talent Controller</h2>
  
  <pre><code>//
  // GET: /Talent/Create
  public ActionResult Create()
  {
      return View();
  }
  
  //
  // POST: /Talent/Create
  [HttpPost]
  public ActionResult Create(Talent talent)
  {
      talent.Modified = talent.Created = DateTime.Now;
  
      if (ModelState.IsValid)
      {
          _db.Talents.Add(talent);
          _db.SaveChanges();
          CreatePhoto(talent.Id);
          return RedirectToAction("Index");
      }
  
      return View(talent);
  }
  </code></pre>
  
  <h2>Updated: Search Controller and Create View</h2>
  
  <pre><code>//
  // GET: /Search/Create
  public ActionResult Create()
  { 
      Search search = new Search();
      search.SearchSet = "test Data";
      return View(search);
  }
  
  //
  // POST: /Search/Create
  [HttpPost]
  public ActionResult Create(Search search)
  {
      search.Created = DateTime.Now;
  
      search.URLParameter = 1435267836;
  
      if (ModelState.IsValid)
      {
          _db.Searchs.Add(search);
          _db.SaveChanges();
          return RedirectToAction("Index");
      }
      return View(search);
  }
  </code></pre>
  
  <p>In view put: <code>@Html.HiddenFor(model =&gt; model.SearchSet)</code></p>