Ok so I am trying to pass the value from the input Quantite in the asp-route-qt, but I can't seem to wrap my head around it. This one asp-route-idBillet
is working just fine but the other isn't. If anyone could help me solve this problem with an example it would be very much appreciated. Thanks in advance
Edit: I should of specified that the model I am using for the view can't have Quantite as an attribute. The model contains four types of tickets. Each ticket contains a title, a description, a price and an Id. Basically what this means is that I can't use an asp-for
tag for the input Quantite.
Here's my view:
<form method="POST">
<div >
@foreach(var billet in Model)
{
<br>
<div >
<div >
<div >
<h5 asp-for="Titre">@billet.Titre</h5>
<p asp-for="Descript">@billet.Descript</p>
<label for="Quantite">Quantité:</label>
<input type="number" name="Quantite" id="Quantite">
<a asp-controller="Billet" asp-action="AjouterBillet" asp-route-idBillet="@billet.Id" asp-route-qt>Ajouter au panier</a>
</div>
</div>
</div>
<br>
}
</div>
<br>
<input type="submit" value="Aller au panier" >
</form>
Here's my controller action
private readonly ZooDbContext _zooDbContext;
private readonly Panier _panier;
public BilletController(ZooDbContext zooDbContext, Panier panier)
{
_zooDbContext = zooDbContext;
_panier = panier;
}
public IActionResult AjouterBillet(int idBillet, int qt)
{
Billet billet = _zooDbContext.Billet.Find(idBillet);
_panier.Ajouter(billet, qt);
return RedirectToAction("Liste");
}
CodePudding user response:
Here's what I found that works.
Here's the view:
<form method="post">
<div >
@foreach(var billet in Model)
{
<br>
<div >
<div >
<div >
<h5 asp-for="Titre">@billet.Titre</h5>
<p asp-for="Descript">@billet.Descript</p>
@using (Html.BeginForm("AjouterBillet", "Billet"))
{
<label for="Quantite">Quantité:</label>
<input type="number" name="Quantite" id="Quantite">
<input value="Ajouter au panier" type="submit" asp-controller="Billet" asp-action="AjouterBillet"
asp-route-idBillet="@billet.Id">
}
</div>
</div>
</div>
<br>
}
</div>
<br>
<a asp-controller="Panier" asp-action="AfficherPanier">Aller au panier</a>
</form>
Here's is the action:
public class BilletController : Controller
{
private readonly ZooDbContext _zooDbContext;
private readonly Panier _panier;
public BilletController(ZooDbContext zooDbContext, Panier panier)
{
_zooDbContext = zooDbContext;
_panier = panier;
}
[HttpPost]
public IActionResult AjouterBillet(int idBillet, int Quantite)
{
Billet billet = _zooDbContext.Billet.Find(idBillet);
_panier.Ajouter(billet, Quantite);
return RedirectToAction("Liste");
}
}