'OnGetASync, a model object containing ICollections, and 'saving' the contents between calls (ASP NET 6)

so i am following MS' example here:

https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/complex-data-model?view=aspnetcore-6.0&tabs=visual-studio

in short, i implemented a variant of the code they posted:

using ContosoUniversity.Models;
using ContosoUniversity.Models.SchoolViewModels;  // Add VM
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Threading.Tasks;

namespace ContosoUniversity.Pages.Instructors
{
    public class IndexModel : PageModel
    {
        private readonly ContosoUniversity.Data.SchoolContext _context;

        public IndexModel(ContosoUniversity.Data.SchoolContext context)
        {
            _context = context;
        }

        public InstructorIndexData InstructorData { get; set; }
        public int InstructorID { get; set; }
        public int CourseID { get; set; }

        public async Task OnGetAsync(int? id, int? courseID)
        {
            IQueryable<Instructor> Instructor = from s in _context.Instructor
                                       select s;
            // bunch of code for sorting and crap.

      var pageSize = Configuration.GetValue("PageSize", 150);
            InstructorData.Instructors = await PaginatedList<Instructor>.CreateAsync(
                InstructorsIQ
                //.AsNoTracking()
                , pageIndex ?? 1, 150);

 if (id != null)
            {
  InstructorID = id.Value;
                var selectedInstructor = InstructorData.Instructors.SingleOrDefault(i => id.Value == i.ID);

                InstructorData.Courses = instructor.Courses;

               
            }

            if (CourseID != null)
            {
                CourseID = AbID.Value;
                var selectedCourse = InstructorData.Courses.SingleOrDefault(i => i.CourseID == CourseIDId);
                await _context.Entry(selectedCourse).Collection(x => x.Enrollment).LoadAsync();
                InstructorData.Enrollment = selectedCourse.Pitches;


            }
        }
    }
}

where mine is much cooler because it can sort and all these other things.

one question i had is about the model variable

        public InstructorIndexData InstructorData { get; set; }

i thought if instantiated this variable outside OnGetAsync

        public InstructorIndexData InstructorData { get; set; } = new InstructorIndexData();

and commented out the corresponding new call at the beginning of the function, that i would be able to 'store' the contents across function calls.

it turns out this is not the case.

i need to use FromSqlRaw for some of the queries i'm running, but i want to avoid doing that if the previous selected value is the same as the current one.

in the example above, it would be something like choosing the same instructor, or course taught by them

enter image description here

is there any specific reason why InstructorData.Courses would be null on the next call to OnGetAsync? is there something wrong with how i'm modeling?

from what i can see all the foreign keys and relationships are set up correctly.

i admit i'm new to this whole situation and could be making a super simple mistake.

i just don't want to run queries again if the previous one was the same selection.



Sources

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

Source: Stack Overflow

Solution Source