Question

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

Was it helpful?

Solution

Don't use the foreach. EditorTemplates already iterate over collections if you pass it a collection.

@model MyNamespace.ExerciseForPupil

@using (Html.BeginForm("ScoreExercise", "SharedLesson"))
{
    @Html.HiddenFor(x => x.Id)
    @Html.EditorFor(x =>  x.ExerciseItemsForPupil)
    <input type="submit" value="Submit"/>
}

A few things to note. You don't have to pass the template name, as the template name is already the same name as the item type. You don't have to use the Post formmethod, as that's the default. There is no name of the property for List so I just assumed it was the plural.

Your last class is also illegal, you would not specify it as a List like that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top