使用MVC编辑集合中的集合

本文关键字:集合 编辑 MVC 使用 | 更新日期: 2023-09-27 18:15:16

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

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}

我已经创建了配置文件视图与@model CollectionEditing.Models.Profile和两个部分视图作为PhaseEditor和SubPhaseEditor

我的配置文件视图,我使用部分视图:

//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]);
    }
}

编辑视图创建成功。

当我点击提交按钮时,在我的动作中,我得到了配置文件的值及其阶段列表,而我没有得到阶段类中的子阶段列表。子阶段列表设置为null。

我也试过把Html.RenderPartial("SubPhaseEditor", Model.Phases[i].SubPhases[j]);放在部分视图PhaseEditor中,但仍然不起作用。

请帮…我如何也能得到子阶段的列表。

非常感谢你的帮助。

Sushil

使用MVC编辑集合中的集合

你的意思是ModelBinder没有传递一个'full'对象给你的控制器?

这不会自动为您完成。ModelBinder不使用底层集合。你必须自己创建这个集合。在你的控制器中使用FormCollection作为参数,并迭代它来创建集合。

这篇文章展示了如何通过FormCollection枚举:如何在ASP中枚举formcollection ?净MVC吗?