在一个视图中编辑视图模型列表
本文关键字:视图 编辑 模型 列表 一个 | 更新日期: 2023-09-27 17:50:23
我正在尝试将视图模型列表传递给一个视图:
这是我想传递的:
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,但取消列表总是空的。我做错了什么?这是因为列表取消类本身也有其他类吗?
视图顶部的@model声明需要用Ienumerable
封装@model IEnumerable<Project.ViewModels.CancellationsVM>