I have these codes that displays the first student record and first group record in my database in the Index page:
var studentToAdd = db.Students.FirstOrDefault();
var selectedGroup = db.Groups.FirstOrDefault();
ViewBag.Name = studentToAdd.Firstname;
ViewBag.Group = selectedGroup.GroupName;
It works and it displays "Richard" and "Group1" in my index page. But when I add this code that should add "Richard" to "Group1" I get a null object exception :
selectedGroup.Students.Add(studentToAdd);
How do i fix this? thanks