0

Here is my Html.BeginForm code. I want to pass 2 values to the Action CheckOutProduct.
How can I trigger Html.BeginForm by clicking a button?

@Html.BeginForm("CheckOutProduct", "CheckOut")
{
    <input type="hidden" id="productCodeForCheckout" value="" />
    <input type="hidden" id="productQtyForCheckout" value="0" />
}

<button class="btn" id="checkout"><span>add to cart</span></button>
AlexB
  • 7,302
  • 12
  • 56
  • 74
jkl
  • 675
  • 2
  • 8
  • 23

3 Answers3

3

Just place a submit button inside your form

@Html.BeginForm("CheckOutProduct", "CheckOut")
{
    <input type="hidden" id="productCodeForCheckout" value="" />
    <input type="hidden" id="productQtyForCheckout" value="0" />

    <input type="submit" class="btn" id="checkout" title="Add to Cart" />
}

If you prefer to stick to button instead of input, just set the type to submit

<button type="submit" class="btn" id="checkout">...</button>
James
  • 80,725
  • 18
  • 167
  • 237
2

You should add a submit button to the form

@Html.BeginForm("CheckOutProduct", "CheckOut")
{
 <input type="hidden" id="productCodeForCheckout" value="" />
 <input type="hidden" id="productQtyForCheckout" value="0" />
 <input type="submit">Add to cart</button>
}
ssilas777
  • 9,672
  • 4
  • 45
  • 68
0

You can also add FormMethod.Post, a HTTP method for processing the form, either in a form of GET or POST, which in your case is POST method. This will make sure that your method in your controller will fire after Post:

@Html.BeginForm("CheckOutProduct", "CheckOut", FormMethod.Post)
{
  <input type="hidden" id="productCodeForCheckout" value="" />
  <input type="hidden" id="productQtyForCheckout" value="0" />

  <input type="submit" class="btn" id="checkout" title="Add to Cart" />
}
Willy David Jr
  • 8,604
  • 6
  • 46
  • 57
  • The default is `FormMethod.Post` so adding that parameter is not required. –  Nov 10 '16 at 02:09