views:

45

answers:

3

Hi everyone,

I currently have a repository setup that loads forms a person would need to fill out when a view controller action is accessed... the way i have it setup is as follows:

public ActionResult View(long id)
    {
        Patient patient = patientRepo.GetPatient(id);
        if (patient == null)
            return View("NotFound");
        var forms = formRepo.GetFormsForPatient(id);
        return View(new PatientViewModel(patient, forms));
    }

the viewmodel is:

public class PatientViewModel
{
    //properties
    public Patient Patient { get; set; }
    public IQueryable<Form> Forms { get; set; }
    //constructor
    public PatientViewModel(Patient patient, IQueryable<Form> forms)
    {
        Patient = patient;
        Forms = forms;
    }
}

and finally my forms repo method looks like this:

public IQueryable<Form> GetFormsForPatient(long id)
    {
        List<Form> formsReturn = new List<Form>();
        var forms = from form in db.Forms
                    where form.PatientID == id
                    select form;
        foreach (var form in forms)
            formsReturn.Add(form);
        return formsReturn.AsQueryable();
    }

the problem is when i try to loop through my forms object and access some relational data to the forms table (the forms table holds the relation between form and patient, there is another table holding form data that needs to be loaded) I get a null reference when trying to do something like

<% foreach (var form in Model.Forms)
       { %>
        <p>Form: <%= Html.Encode(form.FormTextBank.FormName) %></p>
        <p><%= form.FormTextBank.FormText %></p>
    <% } %>

can anyone spot anything wrong?

A: 

Have you disabled change tracking in Linq-to-Sql by any chance? I've found that relational data will not come automatically if that's the case.

http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.objecttrackingenabled.aspx

Matt Sherman
no i haven't disabled change tracking
Jimmy
it is loading my patient data however...
Jimmy
A: 

Found the problem... An incorrectly filled out ID in a table lead to no results

Jimmy
A: 

The patient with patientId equals to the paramenter, has any form?

eKek0