【问题标题】:How to use HttpContext to obtain value of int instead of string?如何使用 HttpContext 获取 int 而不是 string 的值?
【发布时间】:2014-05-13 21:16:17
【问题描述】:

我有当前行从文本框中请求产品#:

 string[] productNumbers = HttpContext.Current.Request.Form.GetValues("ProductNumber");

我尝试对其进行调整以从 Qty 中获取值:

 int[] qty = HttpContext.Current.Request.Form.GetValues("quantity_input");

但是我收到一个错误,它无法将字符串转换为 int。那么我怎样才能使用这个去获得一个 Int 呢?我只是想抓住它。

“CS0029:无法将类型'string[]'隐式转换为'int'”

我的完整代码是:

CartItemCollection items = new CartItemCollection();
Cart cart = Core.GetCartObject();
string skus = "";
string debugStr = "";
Product product = null;
int[] qty = HttpContext.Current.Request.Form.GetValues("quantity_input");
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;
}

【问题讨论】:

  • 您可以为您的问题添加更多信息吗? C# 还是 Java ?哪一行引发了错误。等等。您也添加了一个数据样本。

标签: c# java asp.net forms web-services


【解决方案1】:

那么你将不得不将字符串值转换为整数,例如

List<int> qty = new List<int>();
foreach (string item in HttpContext.Current.Request.Form.GetValues("quantity_input"))
{
    qty.Add(int.Parse(item));
}

如果您想防范非数字值,请使用 TryParse

【讨论】:

  • 我收到错误:CS0246:找不到类型或命名空间名称“var”(您是否缺少 using 指令或程序集引用?)
  • Var 是一种隐式类型(类型的别名)。在这里你可以用“字符串”替换它。
  • @aloisdg 我刚试过,收到的错误是:CS0029:无法将类型'System.Collections.Generic.List'隐式转换为'string'
  • 更新删除var,大概你用的是<.net>
  • @AlexK。阅读另一个答案:他正在使用 .NET 2
【解决方案2】:

它总是会返回一个string[],你可以使用下面的Linq 语句将string[] 转换为一个int[]。

string[] result = HttpContext.Current.Request.Form.GetValues("quantity_input");
int[] quantityInputs = result.Select(x => int.Parse(x)).ToArray();

【讨论】:

  • 对于第二行,我收到错误:CS1525: Invalid expression term '>' Version Information: Microsoft .NET Framework Version:2.0.50727.5477; ASP.NET 版本:2.0.50727.5479
  • 您在使用 System.Linq 程序集吗?
猜你喜欢
  • 1970-01-01
  • 2010-11-13
  • 1970-01-01
  • 2022-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多