I am working a project and I had some trouble. I want to send id from html beginform
but I couldn't do it.
I want to send /BuyTourTicket/tourid
This is my code:
public ActionResult TourTicket(int id)
{
var tour = db.TBLTUR.Find(id);
ViewBag.tourid = tour.id;
ViewBag.tourname = tour.tur_basligi.ToString();
ViewBag.kalkisYeri = tour.kalkis_yeri.ToString();
ViewBag.tarih = tour.tarih.ToString();
ViewBag.detaylar = tour.detay.ToString();
ViewBag.turYetkilisi = db.TBLTURYETKILISI.Find(id).ad " " db.TBLTURYETKILISI.Find(id).soyad;
return View("TourTicket",tour);
}
public ActionResult BuyTourTicket()
{
return View();
}
[HttpPost]
public ActionResult BuyTourTicket(int id)
{
TBLTURREZERVASYON reservation = new TBLTURREZERVASYON();
reservation.tur = id;
db.TBLTURREZERVASYON.Add(reservation);
db.SaveChanges();
return View();
}
This is the error I get:
CodePudding user response:
The first, the default route usually describes the id
parameter as optional. Therefore change the action method declaration to public ActionResult BuyTourTicket(int? id)
:
[HttpPost]
public ActionResult BuyTourTicket(int? id)
{
TBLTURREZERVASYON reservation = new TBLTURREZERVASYON();
reservation.tur = id;
db.TBLTURREZERVASYON.Add(reservation);
db.SaveChanges();
return View();
}
The second, change the parameters order in the Html.BeginForm()
of the TourTicket.cshtml
to:
@using (Html.BeginForm("BuyTourTicket", "Tur", new { id = ViewBag.tourid }, FormMethod.Post))
The route values are third parameter and the HTTP method is the fourth parameter.
CodePudding user response:
I would fix an action route
[HttpPost("{id"})]
public ActionResult BuyTourTicket(int id)
and add Get to another action
[HttGet]
public ActionResult BuyTourTicket()
{
return View();
}