title: ModelState.IsValid ignores validation of the non-nullable property in MVC
body: <p>I cannot work out why does the <strong>Id</strong> property of my model not get validated by the modelstate.</p>
  
  <p>This happens in a Create action, where the <strong>Id</strong> doesn't get returned from the view to the model, as it is only about to be generated. </p>
  
  <p>The bottom line is, naturally, a desirable outcome, as validating a missing <strong>Id</strong> would only cause problems, but I cannot find any instructions which would tell <strong>modelstate</strong> to skip this property and I'd be keen to understand it better for later reuse.</p>
  
  <p>I will also add, that all the properties of this model are marked as required.</p>
  
  <p>EDIT:
  The indicated duplicate, points to a different problem.</p>
  
  <p>What I am tryign to point at here, is that the ID attribute, does not go through validation at all, and not the fact that it passes the validation. Please see the modelstate pop-up box in attached screenshot, the modelstate does not look at the Id property at all.</p>
  
  <p>Here's how it looks like in debugging:
  <a href="https://i.stack.imgur.com/7N7kz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7N7kz.png" alt="enter image description here"></a></p>
  
  <p>The screenshot controllers is this:</p>
  
  <pre><code>[HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Edit([Bind(Include = "Id,Name,ClientName")] Project project)
      {
          if (ModelState.IsValid)
          {
              //db.Entry(project).State = EntityState.Modified;
              db.MarkAsModified(project);
              db.SaveChanges();
              return RedirectToAction("Index");
          }
          return View(project);
  </code></pre>
  
  <p>My model is this:</p>
  
  <pre><code>public class Project
  {
      [Required]
      public int Id { get; set; }
  
      [Required]
      [Display(Name = "Project name")]
      public string Name { get; set; }
  
      [Required]
      public string ClientName { get; set; }
  }
  </code></pre>
  
  <p>And the view that returns the user input is this:</p>
  
  <pre><code>@model Freelancer_DeveloperChallenge.Models.Project
  
  @{
      ViewBag.Title = "Create";
  }
  
  &lt;h2&gt;Create&lt;/h2&gt;
  
  
  @using (Html.BeginForm()) 
  {
      @Html.AntiForgeryToken()
  
      &lt;div class="form-horizontal"&gt;
          &lt;h4&gt;Project&lt;/h4&gt;
          &lt;hr /&gt;
          @Html.ValidationSummary(true, "", new { @class = "text-danger" })
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.Name, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.Name, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.ClientName, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.ClientName, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.ClientName, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              &lt;div class="col-md-offset-2 col-md-10"&gt;
                  &lt;input type="submit" value="Create" class="btn btn-default" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  
  @section Scripts {
      @Scripts.Render("~/bundles/jqueryval")
  }
  </code></pre>