asp.net 为什么在cshtml文件中没有下拉菜单?

dbf7pr2w  于 2022-11-19  发布在  .NET
关注(0)|答案(1)|浏览(176)

下面是显示AddBooking视图的cshtml代码,我在下面的RoomType中没有下拉菜单。

@model Booking
@inject SignInManager<IdentityUser> SignInManager

@{
    var rooms = ViewData["RoomTypes"] as List<SelectListItem>;
}

<h1>Add Booking</h1>
<hr />

@if (SignInManager.IsSignedIn(User))
{
    <div class="row">
        <div class="col-md-6">
            <form asp-action="AddBooking" method="post">
                <div asp-validation-summary="ModelOnly" claclass="text-danger"></div>

                <div class="form-group">
                    <label asp-for="ChecKIn" class="control-label"></label>
                    <input asp-for="ChecKIn" class="form-control" />
                    <span asp-validation-for="ChecKIn" class="text-danger"></span>
                </div>

                <div>
                    <label asp-for="CheckOut" class="control-label"></label>
                    <input asp-for="CheckOut" class="form-control" />
                    <span asp-validation-for="CheckOut" class="text-danger"></span>
                
                </div>
                
//this id is the part where the dropdown is supposed to happen
                <div class="form-group">
                    <label asp-for="RoomType" class="control-label"></label>
                    <select asp-for="RoomTypeId" asp-items="rooms" class="form-control"></select>
                </div>

                <div class="form-group">
                    <label asp-for="NumberOfRooms" class="control-label"></label>
                    <input asp-for="NumberOfRooms" class="form-control" />
                    <span asp-validation-for="NumberOfRooms" class="text-danger"></span>
                </div>

                <br>
                <div class="form-group">
                    <button type="submit" class="btn btn-primary">BookRoom</button>
                </div>
            </form>
        </div>
    </div>
}

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

我正在寻找我犯错误的地方,因为它没有显示下拉选项和房间类型应该从数据库访问,应该能够显示在下拉菜单中,以及如何在房间表中设置房间的可用性假,一旦我们从bOoking视图预订房间?
如果你需要更多的信息,这里是github的链接:
网站github.com/meprigesh/HotelReservationwithsec.git

enyaitl3

enyaitl31#

您需要在AddBooking操作中配置ViewData["RoomTypes"],如下所示,以填充下拉列表:

[Route("api/[controller]/[action]")]
[ApiController]
public class BookingController : Controller
{
    private readonly HotelReservationContext context;
    public BookingController(HotelReservationContext context)
    {
        this.context = context;
    }
    [HttpGet]
    public IActionResult AddBooking()
    {
        ViewData["RoomTypes"] = context.RoomTypes.Select(
                                    c => new SelectListItem
                                    {
                                        Value = c.RoomTypeId.ToString(),
                                        Text = c.TypeOfRoom
                                    }).ToList();
        return View();
    }
}

相关问题