jkl jkl - 25 days ago 8
ASP.NET (C#) Question

How can I trigger Html.BeginForm by clicking a button?

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>

Answer

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>
Comments