如何处理多个对象输入 ASP.NET MVC?

问题描述 投票:0回答:0

我正在创建的网络应用程序应该用于进行酒店预订。一个预订可以有很多客户。创建预订时,如何让用户输入多个对象?

这是我到目前为止想出的:

但问题是模型获得了所有客户,而不仅仅是选定的客户。

我只想传递用户选择的客户而不是每个客户,并且能够实现分页

创建视图:

@model ReservationCreateViewModel

@{
    ViewData["Title"] = "Create Reservation";
}
<div class="row d-flex justify-content-center align-items-center h-100">
    <div class="card rounded-3" style="background-color:#F0F2F5">
        <h1 class="text-center">Create Reservation</h1>
        <div class="card-body p-4 p-md-5">
            <form asp-action="Create">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <div class="form-group">
                    <label asp-for="AccommodationDate" class="control-label"></label>
                    <input asp-for="AccommodationDate" min="@DateTime.Today.ToString("yyyy-MM-dd")" class="form-control" />
                    <span asp-validation-for="AccommodationDate" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="LeaveDate" class="control-label"></label>
                    <input asp-for="LeaveDate" min="@DateTime.Today.ToString("yyyy-MM-dd")" class="form-control" />
                    <span asp-validation-for="LeaveDate" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="HasAllInclusive" class="control-label"></label>
                    <input asp-for="HasAllInclusive" class="form-control" />
                    <span asp-validation-for="HasAllInclusive" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="HasBreakfast" class="control-label"></label>
                    <input asp-for="HasBreakfast" class="form-control" />
                    <span asp-validation-for="HasBreakfast" class="text-danger"></span>
                </div>
                <h4>
                    Available Customers:
                </h4>
                <table class="table table-striped table-hover "
                       style="border-radius: 10px; overflow: hidden;">
                    <thead class="thead-dark">
                        <tr>
                            <th>Id</th>
                            <th>First Name</th>
                            <th>Last Name</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        @for (int i = 0; i < Model.Customers.Count; i++)
                        {
                            <tr>
                                <td>
                                    @Html.DisplayFor(modelItem => Model.Customers[i].Id)
                                </td>
                                <td>
                                    @Html.DisplayFor(modelItem => Model.Customers[i].FirstName)
                                </td>
                                <td>
                                    @Html.DisplayFor(modelItem => Model.Customers[i].LastName)
                                </td>
                                <td>                                   
                                    @Html.CheckBoxFor(model => Model.Customers[i].HasReservation)
                                    @Html.HiddenFor(model => Model.Customers[i].Id)
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
                <a asp-controller="Customers" asp-action="Create" class="btn btn-primary" style="margin: 10px 0px 10px 0px">New Customer <i class="fa-regular fa-square-plus"></i></a>


                <div class="form-group">
                    <label asp-for="RoomId" class="control-label">Room</label>
                    <select asp-for="RoomId" asp-items="@Model.Rooms"></select>
                    <span asp-validation-for="RoomId" class="text-danger"></span>
                </div>

                <div class="form-group">
                    <input type="submit" value="Create" class="btn btn-primary" />
                </div>
            </form>
        </div>
    </div>

</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{
        await Html.RenderPartialAsync("_ValidationScriptsPartial");
    }
}

查看模型:

using Hotel_Reservation_Manager.Data.Models;
using Hotel_Reservation_Manager.ViewModels.Customers;
using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace Hotel_Reservation_Manager.ViewModels.Reservations
{
    public class ReservationCreateViewModel
    {
        public string UserId { get; set; }
        public string CustomerId { get; set; }

        public IList<CustomerIndexViewModel> Customers { get; set; } = new List<CustomerIndexViewModel>();
        //public CustomersIndexViewModel Customers { get; set; } = new CustomersIndexViewModel();
        public string RoomId { get; set; }
        public SelectList Rooms { get; set; }

        [Required]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        [DataType(DataType.Date)]
        [DisplayName("Accomodation date")]
        public DateTime AccommodationDate { get; set; }

        [Required]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        [DataType(DataType.Date)]
        [DisplayName("Leave date")]
        public DateTime LeaveDate { get; set; }

        [DisplayName("Breakfast")]
        public bool HasBreakfast { get; set; }

        [DisplayName("Allinclusive")]
        public bool HasAllInclusive { get; set; }
        [Required]
        public decimal Price { get; set; }
    }
}

控制器:

public async Task<IActionResult> Create()
{
    ReservationCreateViewModel model = new ReservationCreateViewModel()
    {
        Customers = await reservationsService.GetFreeCustomersAsListAsync(),
        Rooms = new SelectList(await reservationsService.GetRoomsSelectListAsync(), "Id", "Number"),
    };
    return View(model);
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(ReservationCreateViewModel model)
{
    if (ModelState.IsValid)
    {
        // Gets current user's id
        model.UserId = User.FindFirstValue(ClaimTypes.NameIdentifier);

        await reservationsService.CreateReservationAsync(model);
        return RedirectToAction(nameof(Index));
    }

    return View(model);
}
asp.net-mvc object view
© www.soinside.com 2019 - 2024. All rights reserved.