ModelState.isValid always false.. .net core Razor page

730 Views Asked by At

Every time I get ModelState.isvalid = false. and the employee.ID is null. On the page-html I use asp-for in a hidden so it should match against id, but still comes out null, anyone have an idea?

      public Employees Employees {get; set;}
     public async Task<IActionResult> OnPost()
    {
        //if (!ModelState.IsValid)
        //{
        //    var errors = ModelState.SelectMany(x => x.Value.Errors.Select(z => z.Exception));
        //}
        if (ModelState.IsValid)
        {
            var dbEmpy = await _db.Employees.FindAsync(Employees.ID);
            dbEmpy.FirstName = Employees.FirstName;
            dbEmpy.LastName = Employees.LastName;
            dbEmpy.Salary = Employees.Salary;
            dbEmpy.isCEO = Employees.isCEO;
            dbEmpy.isManager = Employees.isManager;
            dbEmpy.ManagerId = Employees.ManagerId;
            
            await _db.SaveChangesAsync();

            return RedirectToPage("ListEmpolyee/index");
        }
        return RedirectToPage();
    }
}

and page

         <div class="border container" style="padding:20px;">
<form method="post">
    <input type="hidden" asp-for="Employees.ID" />
    <span class="text-danger" asp-validation-summary="ModelOnly"></span>
    <div class="form-group row">
        <div class="col-4">
            <label asp-for="Employees.FirstName"></label>
        </div>
       
    </div>

     public class Employees
{
    [Key]
    public int ID { get; set; }
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    [Required]
    public decimal Salary { get; set; }
    public bool isCEO { get; set; }
    public bool isManager { get; set; }
    public int? ManagerId { get; set; }
}
0

There are 0 best solutions below