Parameters of a model go to null when put into a database

I'm saving a list of course request models into a student model after getting the data from a view. I can see that the data is all there, but when I save it to the database and access it, the list of course request model is there in the student model but every parameter of the course request is set to null except for the id.

This is the course request model

public class CourseRequest
    {
        [Required]
        public int Id { get; set; }
        public Course FirstChoice { get; set; }
        public Course SecondChoice { get; set; }
        public Class GetClass(List<Class> classes)
        {
            classes = classes.Where(c => c.StudentsEnrolled < c.OccupancyLimit)
                .OrderBy(c => c.StudentsEnrolled).ToList();

            Class FirstClass = GetBestOption(classes, FirstChoice);
            Class SecondClass = GetBestOption(classes, SecondChoice);
            if (FirstClass.course == FirstChoice)
                return FirstClass;
            if (SecondClass.course == SecondChoice)
                return SecondClass;
            if (FirstClass.course.Category == FirstChoice.Category)
                return FirstClass;
            if (SecondClass.course.Category == SecondChoice.Category)
                return FirstClass;

            return FirstClass;
        }

        private Class GetBestOption(List<Class> classes, Course Choice)
        {
            Class Class = classes.FirstOrDefault(c => c.course == Choice);
            if (Class != null)
                return Class;

            Class = classes.FirstOrDefault(c => c.course.Category == Choice.Category);
            if (Class != null)
                return Class;

            return classes[0];
        }
    }

This is the student model

public class Student
    {
        [Required]
        public int Id { get; set; }
        [Required]
        [StringLength(255)]
        public string FirstName { get; set; }
        [Required]
        [StringLength(255)]
        public string LastName { get; set; }
        [Required]
        [Range(8, 12)]
        public int Grade { get; set; }
        [Required]
        public int StudentNumber { get; set; }
        public List<CourseRequest> CourseRequests { get; set; }
        public List<Class> Classes { get; set; }
        public bool DonePickingClasses { get; set; }
        public string UserId { get; set; }
        public Student()
        {
            Classes = new List<Class>();
            CourseRequests = new List<CourseRequest>();
            DonePickingClasses = false;
        }
    }

And this is the method of adding the data to the database

public ActionResult SaveCoursesFor(SelectCoursesViewModel viewModel)
        {
            if (!ModelState.IsValid)
                return new HttpNotFoundResult();

            List<Course> availableCourses = _context.Courses.ToList();
            string Id = User.Identity.GetUserId();
            Student student = _context.Students.Include("CourseRequests").SingleOrDefault(s => s.UserId == Id);
            List<CourseRequest> Requests = new List<CourseRequest>();
            for (int i = 0; i < viewModel.FirstChoices.Count; i++)
            {
                Course firstChoice = availableCourses.SingleOrDefault(c => c.Id == viewModel.FirstChoices[i]);
                Course secondChoice = availableCourses.SingleOrDefault(c => c.Id == viewModel.SecondChoices[i]);

                if (firstChoice == null || secondChoice == null)
                    return new HttpNotFoundResult();

                Requests.Add(new CourseRequest { FirstChoice = firstChoice, SecondChoice = secondChoice });
            }
            student.CourseRequests = Requests;
            student.DonePickingClasses = true;
            _context.SaveChanges();
            return RedirectToAction("Index");
        }

Here is the mdf file properties enter image description here

enter image description here

Here is the view model

public class SelectCoursesViewModel
    {
        public List<Course> AvailableCourses { get; set; }
        public List<int> FirstChoices { get; set; }
        public List<int> SecondChoices { get; set; }
        public int ElectiveCount { get; set; }
        public SelectCoursesViewModel()
        {
            FirstChoices = new List<int>();
            SecondChoices = new List<int>();
        }
    }

1 answer

  • answered 2021-01-19 04:00 tinyDanza

    Please change the property of your .mdf file to Copy to output Directory: "Copy only if newer". If this is not set, your database file with be overwritten every time it runs.