收到错误:
CS0029:无法隐式转换类型' System.Collections.Generic.List'到' int'
不确定如何解决。
我正在使用:
Microsoft .NET Framework版本:2.0.50727.5477;
ASP.NET版本:2.0.50727.5479
给我带来麻烦的部分是:
{
debugStr = debugStr + "-a=noattributesadd";
CartItem item = new CartItem(context);
item.ProductId = product.ProductId;
item.Quantity = qty;
items.Add(item);
}
具体来说,item.Quantity = qty;部分
完整的代码是:
CartItemCollection items = new CartItemCollection();
Cart cart = Core.GetCartObject();
string skus = "";
string debugStr = "";
Product product = null;
List<int> qty = new List<int>();
foreach (string item in HttpContext.Current.Request.Form.GetValues("quantity_input"))
{
qty.Add(int.Parse(item));
}
try
{
string[] productNumbers = HttpContext.Current.Request.Form.GetValues("ProductNumber");
foreach (string productNumber in productNumbers)
{
debugStr = debugStr + "-p=" + productNumber;
if(!string.IsNullOrEmpty(productNumber.Trim()) && !productNumber.StartsWith("Enter Product #"))
{
try
{ //redirect if no product found
product = Core.GetProductObjectByProductNumber(productNumber);
}
catch (Exception e)
{
debugStr = debugStr + "-e=noproductfound";
continue; //do nothing, process the next user input
}
//check if we have a valid product object, allow virtual and other type(s) for adding directly to cart which may need special handling
if(product != null)
{
debugStr = debugStr + "-t=" + product.ProductTypeName;
if(!product.ProductTypeName.Equals("NORMAL"))
{
//assume VIRTUAL (or other type) and redirect for selecting child/group products or other special handling
form.Redirect("product.aspx?p=" + product.ProductNumber);
}
else
{
debugStr = debugStr + "-a=noattributesadd";
CartItem item = new CartItem(context);
item.ProductId = product.ProductId;
item.Quantity = qty;
items.Add(item);
}
skus = skus + ";" + productNumber;
product = null; //reset the product object in case the next product number submitted is invalid
} //product not null
} //sanity check for empty or default data
} //iterate on each product submitted
cart.AddItems(items);
form.Redirect("cart.aspx?skus=" + skus);
}
catch (Exception e)
{
form.AddError("*** ProductNumber provided was not found ***");
form.Redirect("quickorder.aspx?qo=2&e=" + e.Message);
return;
}
基本上,这是快速订单表格的逻辑。我试图将每个项目的数量添加到购物车。
答案 0 :(得分:5)
你的问题在于这一行:
item.Quantity = qty;
item.Quantity
是一个int,qty是List<int>
猜测如何解决(假设所有列表的顺序相同,枚举将以相同的顺序读取它们):
int index = 0; // add this line
foreach (string productNumber in productNumbers)
{
// all the stuff you have already till:
item.Quantity = qty[index];
// all the stuff you have already
index = index + 1;
} //iterate on each product submitted
注意:我讨厌这个解决方案。但它可能会有效。
一个好的解决方案是创建一个数据结构,同时保存产品编号和数量。