MVC - Editing a list of objects

后端 未结 2 809
盖世英雄少女心
盖世英雄少女心 2020-12-10 13:51

I have the following class layout in MVC:

public class ReportModel 
{
    List items;
    string value;
    string anotherValue;
}

相关标签:
2条回答
  • 2020-12-10 14:30

    Kirill's reference to Scott Hanselman's blog entry is correct, but you're reading it too narrowly. In the example shown, he passes the array to the action method, but it could just as easily be contained within the parent model as well. The same concept applies.

    However, one thing to know is that the default model binder does not instantiate nested classes, so it will not create an instance of the List class, which means it will always be null. To fix this, you must instantiate the empty list class in the constructor.

    This is only part of the problem, though as the data must be formatted in the correct way for the model binder to bind it. This is where Scott's blog post comes in, as it provides the format needed for the model binder to recognize the data as a list.

    This is typically handled for you if you use an EditorTemplate and use Html.EditorFor(m => m.Items) and then have a SomeItem.cshtml EditorTemplate. This deals with the issues of collection item naming (so long as you also use strongly typed helpers in the template as well).

    0 讨论(0)
  • 2020-12-10 14:37

    Don't use ElementAt(1) in your lambda expressions => this ruins your input field names. Please read the blog post that Kirill suggested you.

    So you could use indexed access:

    for (int i = 0; i < Model.items.Count; i++)
    {                
        <div class="editrow">
            <div class="edititem">
                <div class="editor-label">
                    @Html.LabelFor(m => m.items[i].propertyOne)
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.items[i].propertyOne)
                    @Html.ValidationMessageFor(m => m.items[i].propertyOne)
                </div>
            </div>
            <div class="edititem">
                <div class="editor-label">
                    @Html.LabelFor(m => m.items[i].propertyTwo)
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.items[i].propertyTwo)
                    @Html.ValidationMessageFor(m => m.items[i].propertyTwo)
                </div>
            </div>
            <div class="edititem">
                <div class="editor-label">
                    @Html.LabelFor(m => m.items[i].propertyThree)
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.items[i].propertyThree)
                    @Html.ValidationMessageFor(m => m.items[i].propertyThree)
                </div>
            </div>
        </div>
    }
    

    Of course in order to have indexer access to the collection this assumes that your items property is declared as either List<SomeItem> or SomeItem[]. If it is an IEnumerable<SomeItem> it won't work. So simply change the type of this property on your view model.

    0 讨论(0)
提交回复
热议问题