title: ASP.NET Core Razor Pages Form Input Validation Not Working
body: <p>So, I am developing a theoretically simple enough questionnaire application with user accounts and I am trying to validate the user input. For example here I am trying to make the First name field Required. I have followed some tutorials and it should be simple but I must be missing something. When I check if my model state is valid to reload the page or post the data, I get an error message telling me that the fields are required even though I have provided a value for them. <em>I have removed some unimportant code parts for clarity.</em> What seems to be the problem here?</p>
  <p><strong>CreateAdmin.cshtml</strong></p>
  <pre><code>@page
  @model ResumePostingService.Pages.CRUD.CreateAdminModel
  @{
      ViewData[&quot;Title&quot;] = &quot;Create Admin&quot;;
      Layout = &quot;~/Pages/SharedPages/_Layout_Admin.cshtml&quot;;
  }
  
  &lt;div&gt;
      &lt;h2&gt;@Model.Messages&lt;/h2&gt;
  &lt;/div&gt;
  
  &lt;h2&gt;Add a new Admin Record&lt;/h2&gt;
  &lt;hr /&gt;
  &lt;div class=&quot;row&quot;&gt;
      &lt;div class=&quot;col-md-4&quot;&gt;
          &lt;form method=&quot;post&quot;&gt;
              &lt;div asp-validation-summary=&quot;ModelOnly&quot; class=&quot;text-danger&quot;&gt;&lt;/div&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;label asp-for=&quot;Admin.FirstName&quot; class=&quot;control-label&quot;&gt;First Name&lt;/label&gt;
                  &lt;input type=&quot;text&quot; asp-for=&quot;Admin.FirstName&quot; class=&quot;form-control&quot; /&gt;
                  &lt;span asp-validation-for=&quot;Admin.FirstName&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;input type=&quot;submit&quot; value=&quot;Create&quot; class=&quot;btn btn-success&quot; /&gt;
              &lt;/div&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;div&gt;
      &lt;a asp-page=&quot;/Admin_Pages/Admin_Index&quot; class=&quot;btn btn-danger&quot;&gt;Cancel&lt;/a&gt;
  &lt;/div&gt;
  
  @section Scripts {
      @{await Html.RenderPartialAsync(&quot;_ValidationScriptsPartial&quot;);}
  }
  </code></pre>
  <p><strong>CreateAdmin.cshtml.cs</strong></p>
  <pre><code>
  namespace ResumePostingService.Pages.CRUD
  {
      public class CreateAdminModel : PageModel
      {
          private readonly ResumePostingService.Models.ResumePostingServiceDatabaseContext _context;
  
          public CreateAdminModel(ResumePostingService.Models.ResumePostingServiceDatabaseContext context)
          {
              _context = context;
          }
  
          readonly DataAccessClass objadmin = new DataAccessClass();
  
          [BindProperty]
          public Admin Admin { get; set; }
  
          public string Messages { get; set; }
  
          public IActionResult OnGet()
          {
              if (HttpContext.Session.GetInt32(&quot;Admin Id&quot;) == null)
              {
                  return RedirectToPage(&quot;/SharedPages/Unauthorized&quot;);
              }
              else
              {
                  return Page();
              }
          }
  
          public ActionResult OnPost()
          {
              if (!ModelState.IsValid)
              {
                  Messages = string.Join(&quot;; &quot;, ModelState.Values
                                          .SelectMany(x =&gt; x.Errors)
                                          .Select(x =&gt; x.ErrorMessage));
  
                  return Page();
              }
  
              objadmin.AdAddAdmin(Admin);
  
              return RedirectToPage(&quot;/Admin_Pages/Admin_Index&quot;, new { actres = 4 });
          }
      }
  }
  </code></pre>
  <p><strong>Admin.cs</strong></p>
  <pre><code>using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using System.ComponentModel.DataAnnotations.Schema;
  
  namespace ResumePostingService.Models
  {
      public partial class Admin
      {
          public Admin()
          {
          }
  
          [Key]
          public int AdminId { get; set; }
  
          [Required(AllowEmptyStrings = false, ErrorMessage = &quot;Please enter the name&quot;)]
          [StringLength(20, MinimumLength = 2, ErrorMessage = &quot;Password cannot be longer than 20 characters and less than 2 characters&quot;)]
          public string FirstName { get; set; }
   
      }
  }
  </code></pre>