Model in model is null so modelstate is invalid

Viewed 31

I have a model in another model. When I try to check the modelstate it is invalid. This is because the other model in this model is null.

Project.cs:

using System.ComponentModel.DataAnnotations;

namespace Toolbox.Models
{
    public class Project
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public WorkPreparation WorkPreparation { get; set; }
    }
}

WorkPreparation.cs:

using System.ComponentModel.DataAnnotations;

namespace Toolbox.Models
{
    public class WorkPreparation
    {
        [Key]
        public int Id { get; set; }
        public int ProjectId { get; set; }
        public CentralHeating CentralHeating { get; set; }
        public Pluming Pluming { get; set; }
        public Ventilation Ventilation { get; set; }
        public Project Project { get; set; }
    }
}

The error I'm getting:

Does anybody have any suggestion?

1 Answers

If you use .net6, try to remove <Nullable>enable</Nullable> in your project.csproj.

Related