I have the following model classes (classes simplifies for the purpose of this question):
public class Lesson
{
public Guid Id {get;set;}
public string Name {get;set;}
public List<ExerciseForPupil> Exercises {get;set;}
}
public class ExerciseForPupil
{
public Guid Id {get;set;}
public string Name {get;set;}
public List<ExerciseItemForPupil> ExerciseItems {get;set;}
}
public class ExerciseItemForPupil
{
public Guid Id {get;set;}
public string Content {get;set;}
public string UserValue {get;set;}
}
Now, I want users to be able to fille "UserValue" value for each exercise in the lesson. Let's say there are 5 exercises for the lesson. I render these exercises as follows
@Html.EditorFor(x=>x.Lesson.Exercises)
Which renders an EditorTemplate which looks as follows: @model MyNamespace.ExerciseForPupil
@using (Html.BeginForm("ScoreExercise", "SharedLesson", FormMethod.Post))
{
@Html.Hidden("Id", Model.Id)
@if (Model.ExerciseItems != null)
{
foreach (var exerciseItem in Model.ExerciseItems)
{
@Html.EditorFor(x => exerciseItem, "ExerciseItemForPupil")
}
}
<input type="submit" value="Submit"/>
}
I also have EditorTemplate for "ExerciseItemForPupil":
@model MyNamespace.ExerciseItemForPupil
@Html.HiddenFor(model => model.Id)
@Html.TextBoxFor(model => model.UserValue)
Problem: As can be seen there will be multiple forms on the page. My "ScoreExercise" action is as follows:
public ActionResult ScoreExercise(ExerciseForPupil exercise)
{
//exercise.ExerciseItems is NULL
}
But my nested collection on the second level (ExerciseItems) is null. What am I doing wrong?
UPDATE I've changed the code according to @MysterMan advices: I call editor template for Exercises as follows:
@Html.EditorFor(x => x.Lesson.Exercises)
and inside this EditorTemplate I call Editor Template for my ExerciseItems
@Html.EditorFor(x=>x.ExerciseItems)
this renders the following markup for UserValue property:
<input id="Lesson_Exercises_0__ExerciseItems_1__UserValue" name="Lesson.Exercises[0].ExerciseItems[1].UserValue" type="text" value="">
but it does not work also