C# 使用MVC 3在集合中编辑集合

C# 使用MVC 3在集合中编辑集合,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,我正在尝试为以下场景创建编辑视图: Profile class { String profileName, IList<Phase> phases; //plus there getter setter} Phase class { String phaseName, IList<SubPhase> subPhases; //plus there getter setter} SubPhase class { String subPhaseName // plus t

我正在尝试为以下场景创建编辑视图:

Profile class { String profileName, IList<Phase> phases; //plus there getter setter}

Phase class { String phaseName, IList<SubPhase> subPhases; //plus there getter setter}

SubPhase class { String subPhaseName // plus there getter setter}
已成功创建可编辑视图

当我在我的操作中单击Submit按钮时,我得到了Profile的值和它的阶段列表,而没有得到阶段类中的子阶段列表。子阶段列表设置为空

我还尝试过放置
Html.RenderPartial(“SubPhaseEditor”,Model.Phases[I].SubPhases[j])在部分视图阶段编辑器中,但它仍然不工作

请帮忙。。。如何获得子阶段列表

非常感谢你的帮助


Sushil

您的意思是ModelBinder没有将“完整”对象传递给您的控制器

这不会自动为您完成。ModelBinder不使用基础集合。您必须自己创建此集合。将控制器中的FormCollection用作参数,并对其进行迭代以创建集合

这篇文章展示了如何枚举FormCollection:

请将第一个块写为正确的C#如果Html字段上的名称标记与集合的名称和模型中的属性完全匹配,则可以自动完成此操作,并且索引器必须正确(从0开始,决不能重复,否则结果只是一个空对象)。IMO枚举formcollection在MVC中是“欺骗”,它违背了模型绑定器的目的。
//iteration the phases
for (int i = 0; i < Model.Phases.Count; i++)
{ 
    //rendering the partial view for Phases
    Html.RenderPartial("PhaseEditor", Model.Phases[i]);   
    //iteration the SubPhases 
    for (int j = 0; j < Model.Phases[i].SubPhases.Count; j++)
    {
        //rendering the partial view for SubPhases
        Html.RenderPartial("SubPhaseEditor", Model.Phases[i].SubPhases[j]);
    }
}