我有下一个观点:
<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>
和行动方法:
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);
}
如何将数量值传递给此操作方法? 感谢
答案 0 :(得分: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" />
}
答案 1 :(得分:1)
使用@Html.BeginForm
将所有数据提交到AddToCart操作。同时添加将提交表单的链接。
答案 2 :(得分:0)
使用jQuery之类的javascript:
<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>