【发布时间】:2015-06-22 14:42:21
【问题描述】:
我正在构建一个购物门户,我需要在其中接受产品数量并将其传递给名为 order 的操作
如上图所示,我为每个产品添加了一个接受数量的文本框,然后我使用以下代码构建了一个操作链接
@Html.ActionLink("Order Now", "OrderNow", "ShoppingCart", new { id = item.prod_id, qty = @quantity }, new { style = "color: white;" })
为了获取数量,我添加了新的 int 数量属性来查看,如
int quantity = 1;
但是当用户更改数量文本框中的文本时如何更新这个数量变量。
下面是我的查看代码:
@Html.TextBox("qty","", new { id=@item.prod_name, placeholder="Qty", style="width:20px; height:15px; font-size:small;" })
<script type="text/javascript">
$('#@item.prod_name').change(function () {
}
});
</script>
@Html.ActionLink("Order Now", "OrderNow", "ShoppingCart", new { id = item.prod_id, qty = @quantity }, new { style = "color: white;" })
这是我的控制器操作方法
public ActionResult OrderNow(int id, int qty)
{
if (Session["cart"] == null)
{
List<Item> cart = new List<Item>();
cart.Add(new Item(p.FirstOrDefault(), qty));
Session["cart"] = cart;
return RedirectToAction("ViewCart", new { ids = p.FirstOrDefault().prod_sub_cat_id });
}
else
{
List<Item> cart = (List<Item>)Session["cart"];
cart[index].quantity = qty;
Session["cart"] = cart;
return RedirectToAction("ViewCart", new { ids = p.FirstOrDefault().prod_sub_cat_id });
}
}
【问题讨论】:
-
您需要使用 javascript/jquery 更新它,或者使用表单发布值。
-
在文本框的 onchange 事件中使用隐藏字段使用 javascript/jquery 更新其值
-
@StephenMuecke 查看我的视图代码并建议我需要更改的地方
-
你的控制器是什么?它是如何发布的?
GET中的数量是如何分配的?您想要实时更新还是仅针对POST.. -
@gerdi 查看我的编辑.. 添加了我的操作方法.. 我需要更新路线以接受两个参数
标签: c# asp.net-mvc views