I am working a project and I have a database or controller problem.
This is my controller part:
[HttpPost]
public ActionResult ConfirmHotelReservation(int? hotelid, int? customerid)
{
TBLHOTELREZERVASYON reservation = new TBLHOTELREZERVASYON();
reservation.hotel = hotelid;
reservation.musteri = customerid;
db.TBLHOTELREZERVASYON.Add(reservation);
db.SaveChanges();
return RedirectToAction("Index");
}
This is the cshtml part:
@using (Html.BeginForm("ConfirmHotelReservation", "Hotel", new { hotelid = TempData["hotel_id"], customerid = Session["customer_id"] }, FormMethod.Post))
{
<label style="color:black"> Başlangıç Tarihi</label>
@Html.TextBoxFor(x => x.baslangic, new { @class = "form-control", @required = "required" })
<br />
<label style="color:black"> Bitiş Tarihi</label>
@Html.TextBoxFor(x => x.bitis, new { @class = "form-control", @required = "required" })
<br />
<label style="color:black">Kişi Sayısı</label>
@Html.DropDownListFor(x => x.kisi_sayisi, (List<SelectListItem>)ViewBag.person, new { @class = "form-control" })
<br />
<button >Onayla</button>
}
hotelid and customerid added but baslangic,bitis and kisi_sayisi not added
enter code here
How can I solve this ?
CodePudding user response:
Problem: baslangic, bitis and kisi_sayisi are not bound to parameter of the controller action method that your request is routed to.
(One possible) Solution:
Part 1: getting the data in the controller
Change your controller method definition to:
public ActionResult ConfirmHotelReservation(int? hotelid, int? customerid, string baslangic, string bitis, string kisi_sayisi )
Part 2: getting the values of those parameters into the database
add the following lines of code to the body of your controller
reservation.baslangic= baslangic;
reservation.bitis= bitis;
reservation.kisi_sayisi = kisi_sayisi ;
Reference:
https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-6.0#targets-1