简体   繁体   中英

MVC3 View Model - List of Complex Objects, containing Lists

How do I bind such a complex model with multiple layers that contain multiple objects? Right now I pass the model to the view - (populating a form / a check box tree) and I would like the exact model back (SubjectSelectionModel) but it's not binding correctly.

Could anyone elaborate on the process I need to take in order to bind these correctly in my view?

View Model:

public class SubjectSelectionModel
{
    public IList<Subject> Subjects { get; set; }
}

Subject Class:

public class Subject
{
    public String Name { get; set; }
    public IList<Bin> Bins { get; set; }

    public Subject()
    {

    }

    public Subject(IList<Course> courses)
    {

    }
}

Bin Class:

public class Bin 
{
    public Subject Subject { get; set; }
    public int Amount { get; set; }

    public IList<Foo> Foos { get; set; }
}

Foo Class:

public class Foo
{
    public int Number { get; set; }
}

This is where Editor Templates come in handy. Rather than messing around with this, you can use simple editor templates to handle all the grunt work for you.

You would create several templates in ~/Views/Shared/EditorTemplates, and then in your primary view it should look like this:

View.cshtml

@model SubjectSelectionModel
@using(Html.BeginForm()) {
    @EditorFor(m => m.Subjects)
    <input type="submit" />
}

Subject.cshtml

@model Subject
@Html.EditorFor(m => m.Name)
@Html.EditorFor(m => m.Bins)

Bin.cshtml (I assume you don't want to render Subject, this would be an infinite loop)

@model Bin
@Html.EditorFor(m => m.Amount)
@Html.EditorFor(m => m.Foos)

Foo.cshtml

@model Foo
@Html.EditorFor(m => m.Number)

Obviously, you may want to change the html formatting to whatever you want, but that's essentially it.

You need a for loop for the objects so MVC can bind using the index in the collection.

Example:

for (int subjectIndex = 0; subjectIndex < Model.Subjects.Count; subjectIndex++) {
    @Html.TextBoxFor(x => x.Subjects[subjectIndex].Name)

    for (int binIndex = 0; binIndex < Model.Subjects.Bins.Count; binIndex++) {
        @Html.TextBoxFor(x => x.Subjects[subjectIndex].Bins[binIndex].Amount)
    }
}

..etc.

I gave a similar response to a similar question, here: Generating an MVC RadioButton list in a loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM