文本框的文本更改事件未更新指定值

Text change event for textbox not updating assigned value

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

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

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

    }

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

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