Reputation: 1297
I have a next view:
<form class="actionButtons">
<input type="number" id="Quantity" name="Quantity" min="1" max="99" value="1" style="width:35px;"/>
@Html.ActionLink("+ Add to cart", "AddToCart", "Cart", new { productId = Model.ProductID, returnUrl = Request.Url.PathAndQuery }, null)
</form>
and action method:
public RedirectResult AddToCart(Cart cart,int productId, string returnUrl)
{
Product product = repository.Products.FirstOrDefault(p => p.ProductID == productId);
if (product != null) cart.AddItem(product, 1);
return Redirect(returnUrl);
}
How pass quantity value to this action method? Thanks
Upvotes: 0
Views: 213
Reputation: 1039498
@using (Html.BeginForm("AddToCart", "Cart", new { productId = Model.ProductID, returnUrl = Request.Url.PathAndQuery }))
{
<input type="number" id="Quantity" name="Quantity" min="1" max="99" value="1" style="width:35px;"/>
<input type="submit" value="+ Add to cart" />
}
Upvotes: 2
Reputation: 2174
Use javascript like JQuery:
<script type="text/javascript">
$(function() {
var $qty = $("#Quantity");
var $form = $qty.closest("form");
var $link = $form.find("a");
$link.click(function(e) {
var $this = $(this);
$this.attr("href", $this.attr("href") + "&qty=" $qty.val());
});
});
</script>
Upvotes: 0
Reputation: 2978
Use @Html.BeginForm
that will submit all data to your AddToCart action. Also add link that will submit form.
Upvotes: 1