Click here to Skip to main content
15,944,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

I have an ASP.NET Core MVC app that contains two entities: Employee and Task. The relationship between these two entities is One-to-many. The information stored for an employee is the full name, email, phone number, date of birth, and monthly salary. The task consists of a title, description, assignee(the employee assigned to work on it), and a due date. When I want to create an employee, ModelState returns me an error: "Tasks is required". because of that error I can't create an employee. Here is the code:

What I have tried:


        public async Task<IActionResult> Create([Bind("FullName,Email,PhoneNumber,DateOfBirth,MonthlySalary")] Employee employee)
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            return View(employee);

[Click and drag to move]

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

        [Required(ErrorMessage = "Enter first and last name")]
        [StringLength(30, ErrorMessage = "Maximum 30 characters")]
        [Display(Name = "Full name")]
        public string FullName { get; set; }

        [Required(ErrorMessage = "Enter your email")]
        [EmailAddress(ErrorMessage = "Invalid Email address")]
        [Display(Name = "Email")]
        public string Email { get; set; }

        [Required(ErrorMessage = "Enter a phone number")]
        [StringLength(20, ErrorMessage = "Maximum 20 characters")]
        [Display(Name = "Phone number")]
        public string PhoneNumber { get; set; }

        [Required(ErrorMessage = "Enter date of birth")]
        [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
        [Display(Name = "Date of Birth")]
        public DateTime DateOfBirth { get; set; }

        [Required(ErrorMessage = "Enter the monthly salary")]
        [Range(30000,300000, ErrorMessage = "Salary must be in range [30000,300000]")]
        [Display(Name = "Monthly salary")]
        public int MonthlySalary { get; set; }

        public virtual ICollection<Task> Tasks { get; set; }

[Click and drag to move]

Does anyone know what the problem is?
Updated 11-Mar-23 6:12am

You have a problem. You're using the database entity objects as view models. DO NOT DO THIS.

For such a simple app, you should have two sets of objects. The Entity classes that describe your database and used to interact with the database, and another set of classes, called view models, that are used to interact with the web UI and supply it with the data it needs to manipulate the database data.

Why is this? A view can require more data, and/or different data, to populate and interact with database data than the database will supply directly, such as text descriptions or integer data or populating choices in a combobox.
Share this answer
Do you have a using statement for :
using System.Threading.Tasks;

I copied your class, pared it down and tried it at: C# Online Compiler & Interpreter - Replit[^]
and it compiled...

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
class Program {
    static void Main(string[] args) {
        Console.WriteLine("Hello, world!");
      Employee e = new Employee();

public class Employee
        public int Id { get; set; }
        public virtual ICollection<Task> Tasks { get; set;}
Share this answer
Graeme_Grant 11-Mar-23 19:56pm    
Why are you doing his homework for him? He deletes the old questions to try and hide what he is doing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900