您可以向表单添加隐藏字段,然后使用客户端代码为其分配数量输入值:
<form method="post" asp-page-handler="AddToCart">
<input type="hidden" name="productId" value="@Model.Product.Id" />
<input type="hidden" name="quantity" value="1" />
<button type="submit" class="btn btn-success">Add to cart</button>
</form>
<script>
document.querySelector('#quantity').addEventListener('change', (e) => {
document.querySelector('[name="quantity"]').value = e.target.value;
});
</script>