【发布时间】:2020-08-04 10:05:10
【问题描述】:
我正在尝试在输入函数中使用操作结果,这是我的代码,我的目标是当用户 Keyup 或 keydown 或在输入中键入数字时,ProductPrice 的总和发生变化。我不知道,哪个事件是正确的为了我的目标,我使用了 onkeydown 和 onkeyup 事件。但没有任何改变。
<td><input type="number" onkeyup="@Url.Action("CountUp","ShoppingCart",new { id = @item.ProductId })"
onkeydown="@Url.Action("CountDown","ShoppingCart",new { id = @item.ProductId })"
value="@item.ProductCount" min="0" style="width:70px"></td>
这是我的控制器
// GET: ShoppingCart
public ActionResult Index()
{
List<ShowShoppingCart> shopcart = new List<ShowShoppingCart>();
if (Session["ShoppingCart"] != null)
{
List<ShopCartItem> shop = Session["ShoppingCart"] as List<ShopCartItem>;
foreach (var item in shop)
{
var product = db.Products.Find(item.ProductId);
shopcart.Add(new ShowShoppingCart()
{
ProductCount = item.ProductCount,
ProductId = item.ProductId,
ProductPrice = product.ProductPrice,
ProductTitle = product.ProductTitle,
Sum = item.ProductCount * product.ProductPrice
});
}
}
return View(shopcart);
}
public ActionResult CountUp(int id)
{
List<ShopCartItem> shop = Session["ShoppingCart"] as List<ShopCartItem>;
int index = shop.FindIndex(s => s.ProductId == id);
shop[index].ProductCount += 1;
Session["ShoppingCart"] = shop;
return RedirectToAction("Index");
}
public ActionResult CountDown(int id)
{
List<ShopCartItem> shop = Session["ShoppingCart"] as List<ShopCartItem>;
int index = shop.FindIndex(s => s.ProductId == id);
shop[index].ProductCount -= 1;
if (shop[index].ProductCount == 0)
{
shop.Remove(shop[index]);
}
Session["ShoppingCart"] = shop;
return RedirectToAction("Index");
}
这是购物车
public class ShopCartItem
{
public int ProductId { get; set; }
public int ProductCount { get; set; }
}
还有一个问题? 当用户在输入中输入数字时,我应该使用哪个事件?改变()?还是其他?
【问题讨论】:
标签: javascript ajax asp.net-mvc model-view-controller