收到错误:
CS0029:无法将类型'System.Collections.Generic.List'隐式转换为'string'
不确定如何解决。
给我带来麻烦的部分是:
string qty = new List<int>();
foreach (var item in HttpContext.Current.Request.Form.GetValues("quantity_input"))
{
qty.Add(int.Parse(item));
}
完整的代码是:
CartItemCollection items = new CartItemCollection();
Cart cart = Core.GetCartObject();
string skus = "";
string debugStr = "";
Product product = null;
string qty = new List<int>();
foreach (var 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 :(得分:3)
string qty = new List<int>();
应该是
var qty = new List<int>();
答案 1 :(得分:2)
您正在尝试将整数列表转换为字符串
string qty = new List<int>();
也许正在做
List<int> qty = new List<int>();
答案 2 :(得分:1)
如果您要List<int>
,那么您的变量类型应为List<int>
而不是string
。
所以改变:
string qty = new List<int>();
要:
List<int> qty = new List<int>();
现在看来您需要所有数量的总和,然后才能做到:
int sum = qty.Sum();