0

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

2
  • Are you navigating to that link, or trying to post the form to that link? Commented Sep 19, 2011 at 15:40
  • Action link renders to link to /Cart/AddToCart and passes GET params productId and returnUrl when follow this link Commented Sep 19, 2011 at 15:55

3 Answers 3

2
@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" />    
}
Sign up to request clarification or add additional context in comments.

Comments

1

Use @Html.BeginForm that will submit all data to your AddToCart action. Also add link that will submit form.

Comments

0

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>

Comments

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.