I have a ViewModel called applicantVm and I am trying to display a list of family's children that are currently registered. I use a stored procedure to return the list but am having difficulty with converting the results to the ViewModel and displaying it.
In my ViewModel:
[Display(Name = "Child First Name")]
[Required]
public string ChildFirstName { get; set; }
[Display(Name = "Child's Date Of Birth")]
public DateTime ChildDateOfBirth { get; set; }
[Required]
public string Grade { get; set; }
[Display(Name = "Grade Entering")]
[Required]
public int SelectedGradeId { get; set; }
public IEnumerable<SelectListItem> Grades { get; set; }
public IEnumerable<StudentVm> Students { get; set; }
in my controller
public ActionResult Application()
{
var currentUserEmail = User.Identity.Name;
var familyMembers = _db.sp_ApplicantFamily_SelectFamilyMembers(User.Identity.Name).ToList();
List<ApplicantVm> applicantVmList = new List<ApplicantVm>();
ApplicantVm applicantVm = new ApplicantVm();
foreach (var child in familyMembers)
{
//Here is where I get an error
foreach ( var student in applicantVm.Students)
{
student.FirstName = child.FirstName;
}
}
return View();
}
I get an error at applicantvm.student:
Object reference not set to an instance of an object.
How do I set all the Children's properties to student properties?