0

It seems I'm tangled up in broad daylight:

I have a model classes

public class Pilot
{
    //.. other prop-s escaped
    private List<FlightHoursEntry> FlightHours { get; set; }
}

public class FlightHoursEntry
{
    public string Description { get; set; }

    public int Hours { get; set; }
}

Views are listed bleow, all is displayed correctly, but on postback FlightHours property is null, Why the engine doesn't initialize the Pilot's object correctly?

in the PilotEditView I'm using @Html.EditorFor(model => model.FlightHours)

FlightHoursCollectionView is:

@model List<FlightHoursEntry>

@for (int i = 0; i < Model.Count; i++){
FlightHoursEntry fh = Model[i];
@Html.Partial("~/../FlightHoursEntryEditView.cshtml", fh);}

also I've tried this way @Html.EditorFor(model=>model[i], "FlightHoursEntryEditView", fh)

and the simple FlightHoursEntryEditView

@model PumaMvc.Models.BusinessObjects.Copa.FlightHoursEntry

    <div class="editor-label">
        @Html.LabelFor(model => model.Hours)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Hours)
        @Html.ValidationMessageFor(model => model.Hours)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Description)
    </div>
    <div class="editor-field">      
        @Html.TextAreaFor(model => model.Description)           
        @Html.ValidationMessageFor(model => model.Description)
    </div>
Artem Vertiy
  • 1,002
  • 15
  • 31
  • You don't need the @ symbol in the second code segment before `Html.Partial(..` because you are already in a C# context – Zasz Aug 19 '11 at 11:21

2 Answers2

0

The filed is marked private. I suspect the field needs to be public for the MVC ModelBinder to Bind incoming values to it.

EDIT :

If the above does not work :

Also check this out : http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx Phil blogs about how to modelbind to a list of complex objects, you will need to change the editor for FlightHoursEntry class to reflect as it is shown over there. Simply render the value of i within square braces in the final HTML.

There is also an example there, but the post is a little bit out of date, and has MVC2 being used everywhere.

Zasz
  • 12,330
  • 9
  • 43
  • 63
0

Check out the following post. Really helped me on my way to understanding how to implement this - Return a List<E> from a view in view model

Community
  • 1
  • 1
ct5845
  • 1,428
  • 3
  • 16
  • 20