'Form Validation not working when using inheritance asp.net core 3.1

I am a beginner in asp.net core just started an EmployeeManagement project where Employee is my base class:

public class Employee
{
    public int Id { get; set; }

    [Required]
    [Display(Name="First Name")]
    public string Name { get; set; }

    [Required]
    [Display(Name="Official Email ID")]
    [RegularExpression(@"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$",
    ErrorMessage = "Invalid email format")]
    public string Email { get; set; }
    
    public string Department { get; set; } 
}

DropdownListModel is my second class which inherits from base class employee:

public class DropdownListModel:Employee
{ 
    public SelectList Departmentcollection { get; set; }
}

Departmentcollection is used to fill items in to a dropdownlist from database when I create a new employee.

     <form asp-controller="home" asp-action="CreateEmp" method="post">
        <div class="form-group">

        <div>
            <label asp-for="Name"></label>
            <input asp-for="Name" class="form-control" />
            <span asp-validation-for="Name" class="text-danger"></span>
        </div>

        <div>
            <label asp-for="Email"></label>
            <input asp-for="Email" class="form-control">
            <span asp-validation-for="Email" class="text-danger"></span>
        </div>

        <div>
            <label asp-for="Department"></label>
            
            <select asp-for="Department"
        [email protected] class="form-control"></select>

        </div>
    <div>
    <button type="submit" class="btn btn-dark" >Create</button>
    </div>
     [HttpPost]
    public IActionResult CreateEmp(Employee emp)
    {
        if(!ModelState.IsValid)
        {
            return View();

        }

       Employee NewEmployee= repository.AddEmployee(emp);
        return RedirectToAction("Details",new { Id = NewEmployee.Id });
        
    }
 [HttpGet]
public ViewResult CreateEmp()
    {
       return View (new DropdownListModel { Departmentcollection 
      = new SelectList(
     repository
       .employees.Select(d => d.Department).Distinct()) 
       });
        

       
    }

Code is working fine, dropdownlist is populating and records are inserting.

The problem is the validation is not working.

The error I am getting is:

An unhandled exception occurred while processing the request.
NullReferenceException: Object reference not set to an instance of an object.
AspNetCore.Views_Home_CreateEmp.<ExecuteAsync>b__19_0() in 
CreateEmp.cshtml
[email protected] class="form-control"></select> 
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext.GetChildContentAsync(bool useCachedResult, HtmlEncoder encoder)

What am I doing wrong?

I used virtual and override properties still same error.

services.AddControllersWithViews(options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true); 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source