ASP.NET MVC 4-嵌套集合的EditorTemplate [英] ASP.NET MVC 4 - EditorTemplate for nested collections

查看:54
本文介绍了ASP.NET MVC 4-嵌套集合的EditorTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模型类(为此问题简化了类):

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;}
}

现在,我希望用户能够为课程中的每个练习填写"UserValue"值.假设该课程有5个练习.我将这些练习呈现如下

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)

哪个呈现一个EditorTemplate,其外观如下:@model MyNamespace.ExerciseForPupil

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"/>
}

我也有"ExerciseItemForPupil"的EditorTemplate:

I also have EditorTemplate for "ExerciseItemForPupil":

@model MyNamespace.ExerciseItemForPupil
@Html.HiddenFor(model => model.Id)
@Html.TextBoxFor(model => model.UserValue)

问题:可以看出,页面上将有多种形式.我的"ScoreExercise"操作如下:

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
}

但是我在第二层的嵌套集合(ExerciseItems)为空.我在做什么错了?

But my nested collection on the second level (ExerciseItems) is null. What am I doing wrong?

更新我已经根据@MysterMan的建议更改了代码:我将练习的编辑器模板称为:

UPDATE I've changed the code according to @MysterMan advices: I call editor template for Exercises as follows:

@Html.EditorFor(x => x.Lesson.Exercises)

在此EditorTemplate中,我将我的ExerciseItems编辑器模板称为

and inside this EditorTemplate I call Editor Template for my ExerciseItems

@Html.EditorFor(x=>x.ExerciseItems)

这将为UserValue属性呈现以下标记:

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="">

但它也不起作用

推荐答案

不要使用foreach.如果您将集合传递给它,则EditorTemplates已经在集合上进行了迭代.

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"/>
}

一些注意事项.您不必传递模板名称,因为模板名称已经与商品类型相同.您不必使用Post表单方法,因为这是默认设置.List没有该属性的名称,因此我只是假定它是复数形式.

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.

这篇关于ASP.NET MVC 4-嵌套集合的EditorTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆