MVC - 编辑对象列表

DMC*_*pps 11 asp.net-mvc http-post

我在MVC中有以下类布局:

public class ReportModel 
{
    List<SomeItem> items;
    string value;
    string anotherValue;
}
Run Code Online (Sandbox Code Playgroud)

现在我在这种类型的MVC中创建一个强类型视图,并使可编辑的文本字段编辑每个值,并使用foreach循环填充文本字段以编辑someitem列表中的项目.

当我提交到httppost方法时,单个值在reportmodel对象中返回正常,但列表不会在对象中返回.该怎么做?

当我说httppost时,我指的是MVC发回的方法

[HttpPost]
public ActionResult EditReport(ReportModel report)
{
    // Save the report in here after the update on the UI side
}
Run Code Online (Sandbox Code Playgroud)

查看发布someitem列表的代码

if (Model.items != null && Model.items.Count > 0)
{
    for (int i = 0; i < Model.items.Count; i++)
    {                
        <div class="editrow">
            <div class="edititem">
                <div class="editor-label">
                    @Html.LabelFor(m => m.items.ElementAt(i).propertyOne)
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.items.ElementAt(i).propertyOne)
                    @Html.ValidationMessageFor(m => m.items.ElementAt(i).propertyOne)
                </div>
            </div>
            <div class="edititem">
                <div class="editor-label">
                    @Html.LabelFor(m => m.items.ElementAt(i).propertyTwo)
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.items.ElementAt(i).propertyTwo)
                    @Html.ValidationMessageFor(m => m.items.ElementAt(i).propertyTwo)
                </div>
            </div>
            <div class="edititem">
                <div class="editor-label">
                    @Html.LabelFor(m => m.items.ElementAt(i).propertyThree)
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.items.ElementAt(i).propertyThree)
                    @Html.ValidationMessageFor(m => m.items.ElementAt(i).propertyThree)
                </div>
            </div>
        </div>
    }
}
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 16

不要ElementAt(1)在lambda表达式中使用=>这会破坏输入字段名称.请阅读Kirill建议你的博客文章.

所以你可以使用索引访问:

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>
}
Run Code Online (Sandbox Code Playgroud)

当然,为了让索引器访问集合,这假定您的items属性被声明为List<SomeItem>或者SomeItem[].如果它是一个IEnumerable<SomeItem>它将无法正常工作.因此,只需在视图模型上更改此属性的类型即可.

  • 您的表单上有180个字段,视图是否需要所有字段?如果是这种情况,那么使用视图模型确实不会带来太多好处.因此,在这种情况下,您可以使用自定义编辑器模板.不要在视图中写任何`for`循环.只需用`@ Html.EditorFor(x => x.items)`替换整个循环,然后为`SomeItem`类定义一个自定义编辑器模板 - `〜/ Views/Shared/EditorTemplates/SomeItem.cshtml`.ASP.NET MVC将自动为items集合的每个元素调用此自定义模板,因此模板将被强类型化为`@model SomeItem` (2认同)

Eri*_*sch 2

基里尔 (Kirill) 对斯科特·汉塞尔曼 (Scott Hanselman) 博客条目的引用是正确的,但您的阅读范围太窄了。在所示示例中,他将数组传递给操作方法,但它也可以轻松包含在父模型中。同样的概念也适用。

然而,需要知道的一件事是,默认模型绑定器不会实例化嵌套类,因此它不会创建 List 类的实例,这意味着它始终为 null。要解决此问题,您必须在构造函数中实例化空列表类。

这只是问题的一部分,尽管数据必须以正确的方式格式化以便模型绑定器绑定它。这就是 Scott 的博客文章的用武之地,因为它提供了模型绑定器将数据识别为列表所需的格式。

如果您使用 EditorTemplate 并使用 Html.EditorFor(m => m.Items),然后拥有 SomeItem.cshtml EditorTemplate,通常会为您处理此问题。这解决了集合项命名的问题(只要您还在模板中使用强类型帮助程序)。