在1个视图中编辑ViewModel列表

时间:2014-03-16 01:29:23

标签: c# list asp.net-mvc-4 viewmodel actionresult

我正在尝试将视图模型列表传递到一个视图:

这就是我想要传递的内容:

    public class CancellationsVM
    {
       public IEnumerable<CancellationFull> Cancellations { get; set; }
    }

这就是我传递它的方式

    public ActionResult CancelByDateCont1(FormCollection fm)
    {
        ICollection<CancellationFull> cancellations = repo_cancellation.CreateCancellationsWithCourses();
        CancellationsVM cancellationsVM = new CancellationsVM();
        cancellationsVM.Cancellations = cancellations;

        return View(cancellationsVM);
    }

然后更新一些值,然后保存数据:

@model Project.ViewModels.CancellationsVM
@using (Html.BeginForm("CancelByDateCont2", "Faculty", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <legend>CancellationsVM</legend>
    @foreach (var cancellation in Model.Cancellations)
    {
        <div>
            <p>
                @Html.DisplayFor(modelItem => cancellation.CancellationMessage)
            </p>
        </div>
        <div>
            <p>
                @Html.DisplayFor(modelItem => cancellation.Course)
            </p>
        </div>
        <div>
            <p>
                @Html.DisplayFor(modelItem => cancellation.DateTime)
            </p>
        </div>
        <div>
            <p>
                @Html.DisplayFor(modelItem => cancellation.Faculty)
            </p>
        </div>
        <div>
            <p>
                @Html.DisplayFor(modelItem => cancellation.Id)
            </p>
        </div>

    }

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
}

然后获得更改:

    [HttpPost]
    public ActionResult CancelByDateCont2(CancellationsVM fm)
    {
        return View();
    }

所以我能够收到CancellationsVM fm,但是取消列表总是为空。 我究竟做错了什么?这是因为列表取消类本身还有另一个类吗?

1 个答案:

答案 0 :(得分:0)

视图顶部的@model声明需要包含在Ienumerable

@model IEnumerable<Project.ViewModels.CancellationsVM>