一个视图中的多个模型模型连接 class

multiple models in one view models connecting class

在过去的 2-3 天里,我试图弄清楚如何在一个视图中实现 2 个模型。我在上面做了一个主题,但是当我试图实现它时,代码就是不工作,我得到了很多错误,当我终于得到网站 运行 时,我得到了 InvalidOperationException 错误。 我在索引视图中的模型:

@model IEnumerable<Hotel_Reservations_Manager.Models.Models>

这是我的索引视图页面:

            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Reservations.RoomId)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Reservations.isAllInclusive)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Reservations.Price)
                </th>
                <th>Control</th>
            </tr>
            </thead>
            <tbody>
            @foreach (var itemz in Model)
            {
                <tr>
                    <td>
                        @itemz.Reservations.RoomId
                    </td>
                    <td>
                        @itemz.Reservations.isAllInclusive
                    </td>
                    <td>
                        @itemz.Reservations.Price
                    </td>
                    <td>
                        <a asp-action="Edit" asp-route-id="@itemz.Reservations.ID"><i class="fa fa-pencil-square-o" aria-hidden="true" style="font-size:24px;"></i></a>
                        <form id="delform" method="post" asp-action="Delete" asp-route-id="@itemz.Reservations.ID">
                            <a asp-action="Delete" asp-route-id="@itemz.Reservations.ID" onclick="DelFunc();"><i class="fa fa-trash-o" style="font-size:24px;color:red"></i></a>
                        </form>
                    </td>
                </tr>
            }

这是创建视图中的模型:

@model Hotel_Reservations_Manager.Models.Models

这是创建视图:

        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Reservations.RoomId" class="control-label"></label>
                <input asp-for="Reservations.RoomId" class="form-control" />
                <span asp-validation-for="Reservations.RoomId" class="text-danger"></span>
            </div>
            <div class="form-group form-check">
                <label class="form-check-label">
                    <input class="form-check-input" asp-for="Reservations.isAllInclusive" /> @Html.DisplayNameFor(model => model.Reservations.isAllInclusive)
                </label>
            </div>
            <div class="form-group">
                <label asp-for="Reservations.Price" class="control-label"></label>
                <input asp-for="Reservations.Price" class="form-control" />
                <span asp-validation-for="Reservations.Price" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input style="width: 6rem;" type="submit" value="Create" class="crt-btn" />
                <a style="width: 6rem; text-decoration: none; color:white;" asp-action="Index" class="crt-btn">Back</a>
            </div>
        </form>

我的两个模型class:

public class Models
{
    public ReservationsModel Reservations { get; set; }
    public RoomsModel Rooms { get; set; }
}

我的创建操作的预订控制器(POST):

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create(Models.Models model)
    {
        if (ModelState.IsValid)
        {
            _context.Add(model.Reservations);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        return View(model.Reservations);
    }

创建视图需要类型为 Hotel_Reservations_Manager.Models.Models 的模型项,但是当 ModelState.IsValid 为 false 时,您 return model.Reservations (a item of type Hotel_Reservations_Manager.Models.ReservationsModel) 创建视图,这导致此错误。

您可以在post动作中直接返回模型。

return View(model);

But it seems like my code isn't correct because it doesn't create a new reservation on the POST

因为ModelState.IsVaild为false,表示模型没有通过验证(比如你没有给必填项属性赋值),你应该检查一下.如果您仍然无法解决,请将 ReservationsModelRoomsModel 的代码附加到您的 post.