-2

I am trying to create a Edit View for the following scenario:

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}

I have created Profile View with @model CollectionEditing.Models.Profile and two partial view as PhaseEditor and SubPhaseEditor

I profile view I am using partial view:

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

The Editable View is created successfully.

When I click the Submit button In my action I getting the values for Profile and its list of Phases and I am not getting the list of Subphase in Phase class. list of subphases is set to null.

I have also tried putting Html.RenderPartial("SubPhaseEditor", Model.Phases[i].SubPhases[j]); in partial view PhaseEditor but still it does not work.

Please help... How I can get the list of SubPhases also.

Thank you very much for your help.

Sushil

Eranga
  • 32,181
  • 5
  • 97
  • 96

1 Answers1

0

You mean the ModelBinder is not passing a 'full' object to your controller?

That will not be done automatically for you. Underlying collections are not used by the ModelBinder. You will have to create this collection by yourself. Use FormCollection in your controller as a parameter and iterate through it to create the collection.

This post is showing how to enumerate through the FormCollection: How can a formcollection be enumerated in ASP.NET MVC?

Community
  • 1
  • 1
ChristiaanV
  • 5,401
  • 3
  • 32
  • 42
  • It can be done automatically if the name tags on the Html fields match up exactly to the name of the collection and the properties in the model and the indexors must be correct (start with 0 and never have duplicates, other wise the result is just a null object). IMO enumerating the formcollection is "cheating" in MVC, it defeats the purpose of the model binder. – Mathew Thompson Nov 18 '11 at 10:22