MVC - 编辑对象列表 [英] MVC - Editing a list of objects

查看:125
本文介绍了MVC - 编辑对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC以下客舱布局:

 公共类ReportModel
{
    清单< SomeItem>项目;
    字符串值;
    串anotherValue;
}

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

当我提交httppost方法的奇异值回来了reportmodel对象不错,但列表中没有的对象得到返回。应如何做?

当我说httppost我指的是MVC是张贴回

方法

  [HttpPost]
公众的ActionResult EditReport(ReportModel报告)
{
    //保存在这里报告在UI端更新后
}

查看code张贴someitem名单

 如果(Model.items = NULL&放大器;!&安培; Model.items.Count大于0)
{
    的for(int i = 0; I< Model.items.Count;我++)
    {
        < D​​IV CLASS =editrow>
            < D​​IV CLASS =edititem>
                < D​​IV CLASS =编辑标记>
                    @ Html.LabelFor(M = GT; m.items.ElementAt(我).propertyOne)
                < / DIV>
                < D​​IV CLASS =主编场>
                    @ Html.TextBoxFor(M = GT; m.items.ElementAt(我).propertyOne)
                    @ Html.ValidationMessageFor(M = GT; m.items.ElementAt(我).propertyOne)
                < / DIV>
            < / DIV>
            < D​​IV CLASS =edititem>
                < D​​IV CLASS =编辑标记>
                    @ Html.LabelFor(M = GT; m.items.ElementAt(我).propertyTwo)
                < / DIV>
                < D​​IV CLASS =主编场>
                    @ Html.TextBoxFor(M = GT; m.items.ElementAt(我).propertyTwo)
                    @ Html.ValidationMessageFor(M = GT; m.items.ElementAt(我).propertyTwo)
                < / DIV>
            < / DIV>
            < D​​IV CLASS =edititem>
                < D​​IV CLASS =编辑标记>
                    @ Html.LabelFor(M = GT; m.items.ElementAt(我).propertyThree)
                < / DIV>
                < D​​IV CLASS =主编场>
                    @ Html.TextBoxFor(M = GT; m.items.ElementAt(我).propertyThree)
                    @ Html.ValidationMessageFor(M = GT; m.items.ElementAt(我).propertyThree)
                < / DIV>
            < / DIV>
        < / DIV>
    }
}


解决方案

不要使用的ElementAt(1)在你的lambda前pressions =>此遗址您输入字段名。请阅读博客文章说基里尔建议你。

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

 的for(int i = 0; I< Model.items.Count;我++)
{
    < D​​IV CLASS =editrow>
        < D​​IV CLASS =edititem>
            < D​​IV CLASS =编辑标记>
                @ Html.LabelFor(M = GT; m.items [I] .propertyOne)
            < / DIV>
            < D​​IV CLASS =主编场>
                @ Html.TextBoxFor(M = GT; m.items [I] .propertyOne)
                @ Html.ValidationMessageFor(M = GT; m.items [I] .propertyOne)
            < / DIV>
        < / DIV>
        < D​​IV CLASS =edititem>
            < D​​IV CLASS =编辑标记>
                @ Html.LabelFor(M = GT; m.items [I] .propertyTwo)
            < / DIV>
            < D​​IV CLASS =主编场>
                @ Html.TextBoxFor(M = GT; m.items [I] .propertyTwo)
                @ Html.ValidationMessageFor(M = GT; m.items [I] .propertyTwo)
            < / DIV>
        < / DIV>
        < D​​IV CLASS =edititem>
            < D​​IV CLASS =编辑标记>
                @ Html.LabelFor(M = GT; m.items [I] .propertyThree)
            < / DIV>
            < D​​IV CLASS =主编场>
                @ Html.TextBoxFor(M = GT; m.items [I] .propertyThree)
                @ Html.ValidationMessageFor(M = GT; m.items [I] .propertyThree)
            < / DIV>
        < / DIV>
    < / DIV>
}

为了让这个假设集合索引器访问当然,这属性被声明为列表&LT你的项目; SomeItem> SomeItem [] 。如果它是一个的IEnumerable< SomeItem> 将无法正常工作。所以,简单地改变你的视图模型这个属性的类型。

I have the following class layout in MVC:

public class ReportModel 
{
    List<SomeItem> items;
    string value;
    string anotherValue;
}

now I create a strongly typed view in MVC of this type and make editable text fields to edit each value as well as use a foreach loop to populate text fields to edit the items in the list of someitem.

when I submit to the httppost method the singular values come back fine in the reportmodel object but the list does not get returned in the object. How should this be done?

When I say httppost I am referring to the method that MVC is posting back to

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

View code for posting the list of 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>
    }
}

解决方案

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.

这篇关于MVC - 编辑对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆