MVC:编辑数据的列表 [英] MVC: Editor for a list of data

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

问题描述

我想要做类似的东西是什么解释的在这个问题:我想编辑数据列表

不同的是,该碱是不是列表

(我使用VS 2013,所以它不是旧的东西。)

我的视图模型:

 公共类SampleViewModel
{
    // ...编辑其他属性...    // List属性
    公开名单< SampleListItemViewModel> ITEMLIST {搞定;组; }
}公共类SampleListItemViewModel
{
    //仅用于显示。它有一个ID字段标识行。
    公共myEntity所项目{搞定;组; }    //我要修改此!
    公共BOOL {器isChecked获得;组; }
}

我试图查看:

  @model My.Namespace.SampleViewModel@using(Html.BeginForm())
{
    @ Html.AntiForgeryToken()    < - !......普通的编辑,MVC产生... - >    <! - BEGIN LIST编辑 - >    <表>
        &所述; TR>
            <第i个
                适用
            < /第i
            <! - ... - >
        < / TR>        @foreach(在Model.ItemList VAR DOC)
        {
            @ Html.HiddenFor(modelItem => doc.Document.CRMDocumentId)            &所述; TR>
                &所述; TD>
                    @ Html.EditorFor(modelItem => doc.IsChecked)
                    <! - @ Html.ValidationMessageFor(modelItem => doc.IsChecked) - GT;
                < / TD>
                < - !...其他不可编辑的显示器领域,例如名... - >
            < / TR>
        }    <! - 结束清单编辑 - >    <! - ... - >}

在创建视图显示一切我所想要的,但是当我点击创建的项目列表为空。

修改 - 更多信息

控制器:

 公众的ActionResult创建(INT?ID)
    {
        VAR项目= // ... ...填充
        //我证实,ITEMLIST具有价值。
        返回查看(项目);
    }    [HttpPost]
    [ValidateAntiForgeryToken]
    公众的ActionResult创建(SampleViewModel项)
    {
        //这是空
        无功名单= item.ItemList;        //无视一切,现在        返回查看(项目);
    }


解决方案

  

要结合复杂的对象,我们需要为每个项目提供的索引,而不是依赖项目的顺序上。这将确保我们可以明确地用正确的对象匹配提交的属性。


替换的foreach 环路循环:

  @for(INT I = 0; I< Model.ItemList.Count;我++)
    {
        @ Html.HiddenFor(modelItem => modelItem.ItemList [I] .Document.CRMDocumentId)        &所述; TR>
            &所述; TD>
                @ Html.EditorFor(modelItem => modelItem.ItemList [I] .IsChecked)
                @ Html.ValidationMessageFor(modelItem => modelItem.ItemList [I] .IsChecked)
            < / TD>        < / TR>
    }

注意:


  

请注意,该指数必须是整数的一个完整的序列,从0开始,并以1为每个元素增加


此外,每房产在你不希望用户编辑例如像用户ID等添加 @ Html.HiddenFor(..)该属性否则会公布到服务器,如果为他们创造隐藏字段他们不会被 NULL

有关详细信息,您可以看到<一个href=\"http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx\"相对=nofollow> 这里

另请参阅 模型绑定与列表

I want to do something similar to what is explained in this question: I want to edit a list of data.

The difference is that the base is not a list.

(I'm using VS 2013, so it is not old stuff.)

My view model:

public class SampleViewModel
{
    // ... other properties for editing ...

    // The list property
    public List<SampleListItemViewModel> ItemList { get; set; }
}

public class SampleListItemViewModel
{
    // For display only. It has an ID field to identify the row.
    public MyEntity Item { get; set; }

    // I want to modify this!
    public bool IsChecked { get; set; }
}

My attempted View:

@model My.Namespace.SampleViewModel

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <!-- ... normal editing, MVC generated ... -->

    <!-- BEGIN LIST EDIT -->

    <table>
        <tr>
            <th>
                Is Applicable
            </th>
            <!-- ... -->
        </tr>

        @foreach (var doc in Model.ItemList)
        {
            @Html.HiddenFor(modelItem => doc.Document.CRMDocumentId)

            <tr>
                <td>
                    @Html.EditorFor(modelItem => doc.IsChecked)
                    <!-- @Html.ValidationMessageFor(modelItem => doc.IsChecked) -->
                </td>
                <!-- ... other non-editable display fields, e.g. name ... -->
            </tr>
        }

    <!-- END LIST EDIT -->

    <!-- ... -->

}

When I create the view it shows everything as I want it, but when I click "Create" the item list is null.

EDIT - More Info

Controller:

    public ActionResult Create(int? id)
    {
        var item = // ...populate...
        // I confirmed that ItemList has values.
        return View(item);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(SampleViewModel item)
    {
        // This is null
        var list = item.ItemList;

        // ignoring all else for now

        return View(item);
    }

解决方案

To bind complex objects, we need to provide an index for each item, rather than relying on the order of items. This ensures we can unambiguously match up the submitted properties with the correct object.

Replace the foreach loop with for loop:

@for (int i=0; i<Model.ItemList.Count; i++)
    {
        @Html.HiddenFor(modelItem => modelItem.ItemList[i].Document.CRMDocumentId)

        <tr>
            <td>
                @Html.EditorFor(modelItem => modelItem.ItemList[i].IsChecked)
                @Html.ValidationMessageFor(modelItem => modelItem.ItemList[i].IsChecked)
            </td>

        </tr>
    }

NOTE:

Note that the index must be an unbroken sequence of integers starting at 0 and increasing by 1 for each element

Also for every Property in the list which you don't want user to edit like for example UserId etc add an @Html.HiddenFor(..) for that property otherwise it will posted null to the server and if hidden field created for them they will not be NULL.

For further details you can see HERE

Also refer Model Binding with List

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

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