NOTE:Because IDs in database look perfect it might be just mistake in my razor view when I try to refer to property of the model's property(it is in the end of the post).


I have a class Person in Models.Person.cs it has reference to Surveys:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace WebApplication2.Models {
    public class Person {
        public int Id { get; set; }
        // MANY PROPERTIES BEETWEEN
        [DisplayName("Surveys")]
        public virtual ICollection<Survey> Surveys { get; set; }

    }
}

and I got class Survey which consists of Questions and Every Question has an Answer. These classes are inModels.Survey.cs` and look like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication2.Models {
    public class Question {
        public int Id { set; get; }
        public string QuestionText { set; get; }
        public  ICollection<Answer> Answers { set; get; }
        public string SelectedAnswer { set; get; }
        public virtual Survey Survey { get; set; }
    }
    public class Answer {
        public int Id { set; get; }
        public string AnswerText { set; get; }
        public virtual Question Question { get; set; }
    }
    public class Survey {
        public int Id { set; get; }
        public ICollection<Question> Questions { set; get; }

        public virtual Person Person { get; set; }

    }
}

The Entity Framework database context class looks like:

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser> {


        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false) {
            System.Diagnostics.Debug.WriteLine("CONSTRUCTOR");

        }

        public DbSet<Person> Persons { get; set; }
        public DbSet<Meeting> Meetings { get; set; }
        public DbSet<Status> Statuses { get; set; }
        public DbSet<Survey> Surveys { get; set; }
        public DbSet<Question> Questions { get; set; }
        public DbSet<Answer> Answers { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder) {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }

        public static ApplicationDbContext Create() {

            return new ApplicationDbContext();

        }
    }

I seed the database:

  protected override void Seed(WebApplication2.Models.ApplicationDbContext context) {
            System.Diagnostics.Debug.WriteLine("SEED STARTED");
            var survey = new Survey() { Questions = new List<Question>() };
            if (!context.Surveys.Any()) {


                //the below is hardcoded for DEMO. you may get the data from some  
                //other place and set the questions and answers

                var q1 = new Question() { QuestionText = "What is your favourite language", Answers = new List<Answer>() };
                Answer a1 = new Answer() { AnswerText = "PHP" };
                Answer a2 = new Answer() { AnswerText = "ASP.NET" };
                Answer a3 = new Answer() { AnswerText = "Java" };
                context.Answers.AddOrUpdate(a1);
                context.Answers.AddOrUpdate(a2);
                context.Answers.AddOrUpdate(a3);
                context.Questions.AddOrUpdate(q1);

                q1.Answers.Add(a1);
                q1.Answers.Add(a2);
                q1.Answers.Add(a3);
                survey.Questions.Add(q1);

                var q2 = new Question() { QuestionText = "What is your favourite DB", Answers = new List<Answer>() };
                a1 = new Answer() { AnswerText = "SQL Server" };
                a2 = new Answer() { AnswerText = "MyQL" };
                a3 = new Answer() { AnswerText = "Oracle" };
                q2.Answers.Add(a1);
                q2.Answers.Add(a2);
                q2.Answers.Add(a3);

                context.Answers.AddOrUpdate(a1);
                context.Answers.AddOrUpdate(a2);
                context.Answers.AddOrUpdate(a3);
                context.Questions.AddOrUpdate(q2);

                survey.Questions.Add(q2);

                context.Surveys.AddOrUpdate(survey);
                context.SaveChanges();

            }
            if (!context.Persons.Any()) {
                var persons = new List<Person> { 
                         new Person{FirstName = "John", LastName = "Doe", CellNumber = "123-456-789", SecondaryPhoneNumber = "98873213", Address = "1street 2",BirthDate = DateTime.Now.Date, Pesel = "312312312", Notes = "Annoying", Surveys = new List<Survey>(){survey}},
                         new Person{FirstName = "Anna", LastName = "Doe", CellNumber = "113-456-789", SecondaryPhoneNumber = "98873213", Address = "1street 2",BirthDate = DateTime.Now.Date, Pesel = "548555672", Notes = "Less Annoying", Surveys = new List<Survey>()}
                        };


                persons.ForEach(person => context.Persons.AddOrUpdate(person));
                context.SaveChanges();
            }
}

The produced database looks very good. We can see that Survey(Id=4) is associated with John Doe(Id=6) and that Questions(Id=7,8) are associated with Survey(Id=4) etc. so it looks brilliant.

enter image description here

BUT THEN if I try to display(in Razor View) number of surveys for John Doe(Id = 6) I get 1 which is correct and then number of questions in this survey ( by@Html.DisplayFor(x => survey.Questions.Count) I get A first chance exception of type 'System.NullReferenceException' occurred in System.Web.Mvc.dll and nothing is displayed.

The view:

@model WebApplication2.Models.Person
<hr />
<h2>@Html.DisplayNameFor(model => model.Surveys)</h2>

<input type="button" id="Coll" value="Collapse" onclick="javascript:CollapseDiv()" />
<p>
    @Html.DisplayFor(x => Model.Surveys.Count)
</p>
@foreach (var survey in Model.Surveys) {
    <p>
        @using (Html.BeginForm()) {

            <text>ANK</text>
            @Html.DisplayFor(x => survey.Questions.Count)   //HERE I GET NULL POINTER EXCEPTION
            <input type="submit" />
        }
    </p>
}

<hr />

THE RESULT:

enter image description here

Related posts

Recent Viewed