'passing input value to asp-route
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 class="row">
@foreach(var billet in Model)
{
<br>
<div class="col-sm-6">
<div class="card">
<div class="card-body">
<h5 class="card-title" asp-for="Titre">@billet.Titre</h5>
<p class="card-text" asp-for="Descript">@billet.Descript</p>
<label for="Quantite">Quantité:</label>
<input type="number" name="Quantite" id="Quantite">
<a class="btn btn-secondary" 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" class="btn btn-primary">
</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");
}
Solution 1:[1]
Here's what I found that works.
Here's the view:
<form method="post">
<div class="row">
@foreach(var billet in Model)
{
<br>
<div class="col-sm-6">
<div class="card">
<div class="card-body">
<h5 class="card-title" asp-for="Titre">@billet.Titre</h5>
<p class="card-text" 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" class="btn btn-secondary" asp-controller="Billet" asp-action="AjouterBillet"
asp-route-idBillet="@billet.Id">
}
</div>
</div>
</div>
<br>
}
</div>
<br>
<a class="btn btn-primary" 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");
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | charlesjBernier |
