Home > front end >  Why am I not getting drop down menu in my cshtml file?
Why am I not getting drop down menu in my cshtml file?

Time:11-03

Below is the cshtml code to display AddBooking view and I am not getting drop down in RoomType below.

@model Booking
@inject SignInManager<IdentityUser> SignInManager


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


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

@if (SignInManager.IsSignedIn(User))
{
    <div >
        <div >
            <form asp-action="AddBooking" method="post">
                <div asp-validation-summary="ModelOnly" cla></div>

                <div >
                    <label asp-for="ChecKIn" ></label>
                    <input asp-for="ChecKIn"  />
                    <span asp-validation-for="ChecKIn" ></span>
                </div>

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

                <div >
                    <label asp-for="NumberOfRooms" ></label>
                    <input asp-for="NumberOfRooms"  />
                    <span asp-validation-for="NumberOfRooms" ></span>
                </div>

                <br>
                <div >
                    <button type="submit" >BookRoom</button>
                </div>
            </form>
        </div>
    </div>
}

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

I am looking for where I am making a mistake as it is not showing the dropdown options and the type of room should be accessed from database and should be able to display in dropdown, and how to set availability of rooms in room table false once we book the room from bOoking view?

Here is the link of github if you need more details:

link=https://github.com/meprigesh/HotelReservationwithsec.git

CodePudding user response:

You need configure ViewData["RoomTypes"] in AddBooking action like below to populate the dropdown:

[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();
    }
}
  • Related