1

我正在使用视图模型,然后我何时将其发送到操作结果以使用(修改后的视图模型)

但是在控制器中,我丢失了视图模型中的列表和对象。这是我的看法:

@using PigeonFancier.Models
@model PigeonFancier.Models.InschrijvingModel
@using (Html.BeginForm("UpdateInschrijvingen","Melker",Model))
{
<div>
    <fieldset>
        <table>

            @foreach (var item in Model.inschrijvingLijst)
            {
                <tr>
                    <td>@Html.DisplayFor(model => item.Duif.Naam)</td>                              
                    <td> @Html.CheckBoxFor(model => item.isGeselecteerd)</td>                              
                </tr>
            }
        </table>   
        <input type="submit" value="Wijzigen"/>
    </fieldset>
</div>
}

这是我的控制器,目前它什么都不做,直到我可以从视图中获取完整的视图模型。

public ActionResult UpdateInschrijvingen(InschrijvingModel inschrijvingsModel)

    {
        // inschrijvingsModel is not null, but it creates a new model before 
            it comes here with 
        //Use the model for some updates

        return RedirectToAction("Inschrijven", new { vluchtId = 
                   inschrijvingsModel.vlucht.VluchtId });
    }

这是具有 List 和其他一些对象的模型,这些对象变为空,因为它在从视图返回到 actionresult 时创建了一个新模型

public class InschrijvingModel
{
    public Vlucht vlucht;
    public Duivenmelker duivenmelker;
    public List<CheckBoxModel> inschrijvingLijst { get; set; }

    public InschrijvingModel()
    {
        // Without this i get, No parameterless constructor defined exception. 
        // So it uses this when it comes back from the view to make a new model
    }

    public InschrijvingModel(Duivenmelker m, Vlucht vl)
    {
        inschrijvingLijst = new List<CheckBoxModel>();
            vlucht = vl;
            duivenmelker = m;

        foreach (var i in m.Duiven)

        {
            inschrijvingLijst.Add(new CheckBoxModel(){Duif = i, 
            isGeselecteerd =  i.IsIngeschrevenOpVlucht(vl)});
        }

    }

出了什么问题,我应该如何解决这个问题?

谢谢

4

3 回答 3

0

我通过使用找到了列表的部分解决方案

for 和 [i] 而不是 foreach

但对象仍然为空,即使使用 int Id 而不是对象

也已经尝试使用 Html.hiddenfor 来处理那些对象不起作用

于 2012-07-01T17:29:40.050 回答
0

您需要将所有列表和属性包含到您明确定义的模型中。

在 POST 操作中获取视图模型后,您将执行操作(创建、编辑等),如果您想返回相同的视图(具有发布操作),则需要重新填充所有 List 值(如下拉列表和复选框列表)并将它们设置在您返回的视图模型上。

于 2012-07-01T17:58:52.363 回答
0

本文中使用的方法可能是您的解决方案:

http://erraticdev.blogspot.com/2010/09/preparing-list-of-objects-so-they-can.html

于 2012-07-01T20:46:44.603 回答