我正在为我的班级做一个网上商店项目,但我在删除时遇到一个错误,此行中发生异常if (cart[index].Amount == 1)
它说 mscorlib.dll中发生了'System.ArgumentOutOfRangeException'类型的异常但未在用户代码中处理
private int isExisting(int id)
{
List<Item> cart = (List<Item>)Session["cart"];
for (int i = 0; i < cart.Count; i++)
{
if (cart[i].Cl.Id == id)
return i;
}
return -1;
}
public ActionResult Delete(int idDelete)
{
int index = isExisting(idDelete);
List<Item> cart = (List<Item>)Session["cart"];
if (cart[index].Amount == 1) {
cart.RemoveAt(index);
Session["cart"] = cart;
}else
{
cart[index].Amount--;
Session["cart"] = cart;
}
return View("Order");
}