bambi
bambi

Reputation: 1179

ASP.NET MVC 5 don't getting values from HiddenFor in post method

When I try to read values from HiddenFor Helpers in my post method I get exception of type 'System.NullReferenceException' (Additional information: Object reference not set to an instance of an object. id The name 'id' does not exist in the current context). Why is that so and how can I read those? The line where I get exception is commented.

As one part of my application I need to implement questionnaire for students including general questions and questions for subjects they are attending. Here is the code for controller:

 public class StudentController : Controller
{
    private EFDbContext context;
    private String index;
    public StudentController(string Index = "II-2/10")
    {
        context = new EFDbContext();
        this.index = Index;
    }
    // GET: Student
    public ViewResult Student()
    {
        Student student = context.Students.Single(i => i.Index.Equals(index));
        List<Question> questions = context.Questions.ToList();
        StudentVM model = new StudentVM(student,questions);
        return View(model);
    }

    [HttpPost]
    public ActionResult Student(StudentVM model)
    {
        foreach(SubjectVM subject in model.Subjects)
        {
            foreach(QuestionVM question in subject.Questions)
            {
                Results results = new Results();
                if(subject.Name == null)
                {
                    **//EXCEPTION IS HERE!**
                    results.Question = context.Questions.SingleOrDefault(q => q.ID == question.ID).Text;
                }
                else
                {
                    results.Subject = context.Subjects.SingleOrDefault(s => s.ID == subject.ID).Name;
                    //getting ID of appropriate question in database
                    int questionID = (question.ID - (int)subject.ID) / (int) Math.Pow(10, subject.ID.ToString().Length);
                    results.Question = context.Questions.SingleOrDefault(q => q.ID == questionID).Text;
                }
                results.Answer = context.PossibleAnswers.Single(ans => ans.ID == question.SelectedAnswer).Text;
                context.Results.Add(results);
            }
        }
        context.SaveChanges();
        return View("Completed", model);
    }
}

And view:

@model Questionnaire.Domain.Models.StudentVM

@{
    ViewBag.Title = "Student";
}

@using (Html.BeginForm())
{
    for (int i = 0; i < Model.Subjects.Count; i++)
    {
        @Html.HiddenFor(m => m.Subjects[i].ID)
        <h3>@Html.DisplayFor(m => m.Subjects[i].Name)</h3> // display empty string if no name
        for (int j = 0; j < Model.Subjects[i].Questions.Count; j++)
        {
            <div class="well">
                @Html.HiddenFor(m => m.Subjects[i].Questions[j].ID)
                <h3>@Html.DisplayFor(m => m.Subjects[i].Questions[j].Text)</h3>
                @foreach (var answer in Model.Subjects[i].Questions[j].PossibleAnswers)
                {
                    <div>
                        @Html.RadioButtonFor(m => m.Subjects[i].Questions[j].SelectedAnswer, answer.ID, new { id = answer.ID })
                        <label for="@answer.ID">@answer.Text</label>
                    </div>
                }
                @Html.ValidationMessageFor(m => m.Subjects[i].Questions[j].SelectedAnswer)
            </div>
        }
    }
    <input type="submit" class="btn btn-success" value="Confirm" />
}

Upvotes: 1

Views: 667

Answers (1)

bambi
bambi

Reputation: 1179

I found mistake, it was pretty easy.

if(subject.Name == null)
                {
                    **//EXCEPTION IS HERE!**
                    results.Question = context.Questions.SingleOrDefault(q => q.ID == question.ID).Text;
                }

The problem was in

subject.Name == null 

since I didn't post subject.Name from view to post method. When I change it to

subject.ID == null 

everything is fine.

Upvotes: 1

Related Questions