ModelState IsValid always returns false for DropDownList and byte[] in ASP.Net Core 3.1

Last Reply 22 days ago By sourav688

Posted 26 days ago

Hi,

I attached the code. Its working fine Create employee GET method. But on POST method its show ModelState = False and Attribute Picture and Designation are Invalid in EmployeeClass.

Please solve the problem. I am using Stored Procedure with repositoryClass.

public interface ISchoolMgmtSystemRepository
{
    IEnumerable<EmployeeClass> GetAllEmployee();
    EmployeeClass GetEmployee(int eid);
    EmployeeClass AddEmployee(EmployeeClass newEmployee);
    EmployeeClass UpdateEmployee(EmployeeClass updateEmployee);
    EmployeeClass DeleteEmployee(int eid, EmployeeClass empid);

    IEnumerable<DesignationClass> GetAllDesignation();
    DesignationClass GetDesignation(int ddid);
    DesignationClass AddDesignation(DesignationClass newDesignation);
    DesignationClass UpdateDesignation(DesignationClass updateDesignation);
    DesignationClass DeleteDesignation(int ddid, DesignationClass disg);
} 

 

    public class SchoolMgmtSystemRepository : ISchoolMgmtSystemRepository
    {
	public DesignationClass AddDesignation(DesignationClass newDesignation)
        {
            context.Database.ExecuteSqlRaw("NEW_Designation {0},{1}", newDesignation.DesignationID, newDesignation.Designation);
            return newDesignation;
        }
 	public IEnumerable<DesignationClass> GetAllDesignation()
        {
            return context.DesignationInfo.FromSqlRaw<DesignationClass>("GET_AllDesignation").ToList();
        }

 	public EmployeeClass AddEmployee(EmployeeClass newEmployee)
        {
            context.Database.ExecuteSqlRaw("NEW_Employee {0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14}",
                newEmployee.StaffID, newEmployee.StaffName, newEmployee.FatherName, newEmployee.MotherName, newEmployee.DOB,
                newEmployee.Gender, newEmployee.DOJ, newEmployee.Designation, newEmployee.Qualification, newEmployee.PermanentAddress,
                newEmployee.TemporaryAddress, newEmployee.PhoneNo, newEmployee.MobileNo, newEmployee.Email, newEmployee.Picture);
            return newEmployee;
        }
    }

 

    public class DesignationClass
    {
        [Key]
        [DisplayName("Designation ID")]
        public int DesignationID { get; set; }

        [Required(ErrorMessage = "*")]
        [DisplayName("Designation Name")]
        public string Designation { get; set; }
    }

 

    public class EmployeeClass
    {
        [Key]
        [DisplayName("Staff ID")]
        public string StaffID { get; set; }

        .......

        [Required(ErrorMessage = "*")]
        [DisplayName("Designation")]
        public string Designation { get; set; }

        [Required(ErrorMessage = "*")]
        [DisplayName("Qualification")]
        public string Qualification { get; set; }

        ....

        [Required(ErrorMessage = "*")]
        [DisplayName("Photo")]
        public byte[] Picture { get; set; }
    }

 

    public class EmployeeController : Controller
    {
        private readonly ISchoolMgmtSystemRepository employeerepository;

        public EmployeeController(ISchoolMgmtSystemRepository employeerepository)
        {
            this.employeerepository = employeerepository;
        }

        private void PopulateDesignationDropDownList(object selectedDesignation = null)
        {
            var designationsQuery = from d in employeerepository.GetAllDesignation() orderby d.Designation select d;
            ViewBag.DesignationID = new SelectList(designationsQuery, "DesignationID", "Designation", selectedDesignation);
        }

        // GET: Employee
        public ActionResult EmployeeList()
        {
            var allemployeeInfo = employeerepository.GetAllEmployee();
            return View(allemployeeInfo);
        }

        // GET: Employee/Details/5
        public ActionResult EmployeeDetails(int id)
        {
            var employeeview = employeerepository.GetEmployee(id);
            return View(employeeview);
        }

        // GET: Employee/Create
        public ActionResult CreateEmployee()
        {
            PopulateDesignationDropDownList();
            return View();
        }

        // POST: Employee/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateEmployee(EmployeeClass emp)
        {
            var designation = HttpContext.Request.Form["EmpDesignation"].FirstOrDefault();
            emp.Designation = designation;
            var files = HttpContext.Request.Form.Files;
            foreach (var file in files)
            {
                MemoryStream ms = new MemoryStream();
                file.CopyTo(ms);
                byte[] imageBytes = ms.ToArray();
                emp.Picture = imageBytes;
            }
            try
            {
                if (ModelState.IsValid)
                {
                    var newemployeedetail = employeerepository.AddEmployee(emp);
                    return RedirectToAction(nameof(EmployeeList));
                }
            } 
            catch
            {
                ModelState.AddModelError("", "Unable to save changes. ");
                //return View();
            }
            PopulateDesignationDropDownList(emp.Designation);
            return View(emp);
        }
    }

 

@Html.DropDownList("DesignationID", null, "- Please Designation -", htmlAttributes: new { @class = "form-control", @onChange = "selectedclass(this)" })
@Html.ValidationMessageFor(model => model.Designation, "", new { @class = "text-danger", })
<input type="hidden" id="hidenclass" name="EmpDesignation" class="form-control" asp-for="Designation" />


Posted 22 days ago

Hi, I solved the problem by Removing Required Field Validator. Its work perfectly.