Why doesn't MVC bind my nested collection?

If you got a couple of linqTOsql objects that I am trying to edit through a form.

Firstly, there is a class Stream:

[Table]
public class Stream
{
    [HiddenInput(DisplayValue = false)]
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public long StreamID { get; set; }

    /*other properties removes for brevity*/

    // relationship (one stream to many Stream2FieldTypes) 
    // uses EntitySet<Stream2FieldTypes> and OtherKey for the FK in Stream2FieldTypes 
    private EntitySet<Stream2FieldTypes> _Stream2FieldTypes;
    [System.Data.Linq.Mapping.Association(Storage = "_Stream2FieldTypes", OtherKey = "StreamID")]
    public EntitySet<Stream2FieldTypes> Stream2FieldTypes
    {
        get { return this._Stream2FieldTypes; }
        set { this._Stream2FieldTypes.Assign(value); }
    }

}

Then there is a class Stream2FieldTypes:

[Table]
public class Stream2FieldTypes
{
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public long s2fID { get; set; }

    /*other properties removed for brevity*/

    [Column]
    public long StreamID { get; set; }     // FK




    // relationship (one Stream2FieldTypes to Streams) 
    private EntitySet<Stream> _Stream;
    [Association(Storage = "_Stream", ThisKey = "StreamID")]
    public EntitySet<Stream> Stream
    {
        get { return this._Stream; }
        set { this._Stream.Assign(value); }
    }
}

I am using the linqTOsql repository to get these objects and send them to the view as follows:

    public ActionResult StreamEdit(long id)
    {
        Genesis.Domain.Entities.Stream stream = genesisRepository.Streams.FirstOrDefault(x => x.StreamID == id);

        return View(stream);
    }

All are sent for viewing without problems ... But when the form on the view is submitted, for some reason it is lost Stream.Stream2FieldTypes. I will catch a form submission with this action:

    [HttpPost]
    public ActionResult StreamEdit( long id, Genesis.Domain.Entities.Stream form)
    {

            return View(form);
    }

And a view without a collection Stream2FieldTypes.

The view writes the Stream2FieldTypes types as follows:

<% for (int i = 0; i < (Model.Stream2FieldTypes != null ? Model.Stream2FieldTypes.Count() : 0); i++) %>
<% { %>
    <div class="ListItem">
        Label: <%: Html.EditorFor(x => x.Stream2FieldTypes[i].s2fLabel, new { style="width:100px" })%>
        Required: <%: Html.EditorFor(x => x.Stream2FieldTypes[i].s2fIsRequired)%>
        Field Type ID": <%: Html.EditorFor(x => x.Stream2FieldTypes[i].FieldTypeID, new { style="width:20px;" })%>
        Stream ID: <%: Html.EditorFor(x => x.Stream2FieldTypes[i].StreamID) %>
</div>
<% } %>

Why does mvc default model binding correctly bind a nested collection?

+3
1

POCO . . LINQ, , EntitySet<T>, . , , , , .

:

<%: Html.EditorFor(x => x.Stream2FieldTypes) %>

~/Views/Home/EditorTemplates/Stream2FieldTypes.ascx:

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<SomeNs.Stream2FieldTypes>" 
%>
<div class="ListItem">
    Label: 
    <%: Html.EditorFor(x => x.s2fLabel, new { style="width:100px" }) %>

    Required: 
    <%: Html.EditorFor(x => x.s2fIsRequired) %>

    Field Type ID: 
    <%: Html.EditorFor(x => x.FieldTypeID, new { style="width:20px;" })%>

    Stream ID: 
    <%: Html.EditorFor(x => x.StreamID) %>
</div>

, : .

+2

Source: https://habr.com/ru/post/1767606/


All Articles