-1

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?

Curious-programmer
  • 772
  • 3
  • 13
  • 31

1 Answers1

1

In the ViewModel, you should do something like this; at very least. What you're missing is you haven't initialize the Student list, at any place of code.

private List<string> _goodList = new List<string>();
public List<string> GoodOnes
{   
    get
    { return _goodList;}
    set
    {
        _goodList = value;
    }
}

This is a very basic example and you should be able to apply this to yours.

Pramuka
  • 1,064
  • 1
  • 8
  • 15