文本框的文本更改事件未更新分配的值

时间:2015-04-16 09:25:43

标签: c# asp.net-mvc views

我正在构建一个购物门户网站,我需要接受一定数量的产品并将其传递给名为订单的操作

enter image description here

如上图所示,我添加了一个文本框,用于接受每个产品的数量,然后我使用以下代码构建了一个actionlink

@Html.ActionLink("Order Now", "OrderNow", "ShoppingCart", new { id = item.prod_id, qty = @quantity }, new { style = "color: white;" })

获取数量我添加了新的int数量属性来查看,如

int quantity = 1;

但是当用户更改qty文本框中的文本时如何更新此数量变量。

以下是我的观看代码:

  @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 });
        }

    }

1 个答案:

答案 0 :(得分:1)

你真的不想在这里找到GET方法的链接。您修改数据(并且不希望将其添加到浏览器历史记录中),因此您应该发布数据。对于每个产品,添加一个带有数量文本框的表单元素和“立即订购”操作的提交按钮(如果需要,将其设置为链接样式)

@using (Html.BeginForm("OrderNow", "ShoppingCart", new { id = item.prod_id })
{
  <input type="text" class="???" name="qty" placeholder="Qty" />
  <input type="submit" value="Order Now" class="???" />
}

附注:

  1. 添加类名并使用css而不是包含内联样式 如style="width:20px; height:15px; font-size:small;"
  2. 您也可以使用@Html.TextBox("qty", new { id = "", @class="???", placeholder = "Qty"),但请注意id = "" 删除id属性以防止由于重复而导致的无效html