ASP.NET Core 6 Razor Pages - guide with Model Binding for Nested Collections
I've looked through the documentation and I'm still confused about I'm attempting to set up This might be a silly question, but I'm working on a project and hit a roadblock. I'm working on an ASP.NET Core 6 Razor Pages application, and I'm working with an scenario with model binding for a nested collection in my form. I have a `Course` model that contains a list of `Student` objects. When I submit the form, the `Student` collection is not binding correctly, and I see that the model state is invalid. Here are the relevant parts of my code: ### Models ```csharp public class Course { public int Id { get; set; } public string Title { get; set; } public List<Student> Students { get; set; } } public class Student { public int Id { get; set; } public string Name { get; set; } } ``` ### Razor Page ```html @page @model CourseModel <form method="post"> <input type="text" asp-for="Course.Title" /> @for (int i = 0; i < Model.Course.Students.Count; i++) { <div> <input type="text" asp-for="Course.Students[i].Name" /> </div> } <button type="submit">Submit</button> </form> ``` ### Page Model ```csharp public class CourseModel : PageModel { [BindProperty] public Course Course { get; set; } public void OnGet() { Course = new Course { Students = new List<Student> { new Student() } }; } public IActionResult OnPost() { if (!ModelState.IsValid) { return Page(); } // Save course logic return RedirectToPage("Success"); } } ``` When I submit the form, I receive the behavior message: "The Student field is required" for each student entry. I've double-checked the names of the input fields, and they seem correct. I suspect it might be related to how I'm initializing the `Students` list in the `OnGet` method. I tried explicitly adding multiple `Student` objects to the list, but the scenario continues. Any ideas on how to resolve this or improve the model binding for nested collections in Razor Pages? Any help would be greatly appreciated! The stack includes Csharp and several other technologies. Any feedback is welcome!