0

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 class="btn btn-info">Onayla</button>
}

enter image description here

hotelid and customerid added but baslangic,bitis and kisi_sayisi not added enter image description here

enter image description here

enter code here

How can I solve this ?

2
  • 1
    You aren't setting those values in the first code snippet you posted, how would you expect them to get set? Commented Jul 28, 2022 at 22:02
  • you are right I missed this as I'm just starting out. I solved it thank you very much Commented Jul 28, 2022 at 22:10

1 Answer 1

1

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://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-6.0#targets-1

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much i found another way but this also seems to work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.