简体   繁体   中英

Edit List of ViewModels in 1 View

I am trying to pass list of view models to one view:

This is what I am trying to pass:

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

This is how I pass it

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

        return View(cancellationsVM);
    }

then update some of the values, and then save data back:

@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>
}

and then get the changes:

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

So am I able to receive CancellationsVM fm, but List of Cancellations is always null. What am I doing wrong? Is this because list Cancellation class itself also has another classes?

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

@model IEnumerable<Project.ViewModels.CancellationsVM>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM