昨晚我在这里获得了一些关于获取集合索引的帮助。这是我正在使用的代码。
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 1; i < _prod.ActiveProductItemCollection.Count; i++)
{
sb.Append("<script type='text/javascript'>");
sb.Append("mboxCreate(\"product_productpage_rec{0}\")", i);
sb.Append("\"entity.id=" + _prodID + "\",");
sb.Append("\"entity.categoryId=" + _categoryID + "\",");
sb.Append("\"entity.name=" + _prod.ActiveProductItemCollection[i].Title + "\",");
sb.Append("\"entity.pageURL=" + Request.Url.ToString() + "\",");
//The following value has been taken from the productImageControl code behind.
//Might have to refactor in future as a property of the image control.
string filename = AppSettingsManager.Current.ProductImagePathLarge + _prod.ActiveProductItemCollection[i].Sku
+ AppSettingsManager.Current.ProductImageExtension;
sb.Append("\"entity.thumbnailURL=" + filename + "\",");
sb.Append("\"entity.inventory=" + _prod.ActiveProductItemCollection.Count + "\",");
sb.Append("\"entity.value=" + _prod.ActiveProductItemCollection[i].ActualPrice + "\",");
sb.Append("\"entity.ProductItemID=" + _prod.ActiveProductItemCollection[i].Sku + "\",");
sb.Append("\"entity.addToCartImg=~/Images/Buttons/btn_AddToCartFlat.gif\");<");
//The last line has to be /script. < inserted on prev line. do not change it or bad things will happen.
sb.Append("/script>");
}
this.LiteralMBoxScript.Text = sb.ToString();
我想要做的是在productpage_rec之后获取集合中项目的索引。
例如,第一个产品会产生类似
mboxCreate( “product_productpage_rec1”
等等,对于集合中的每个产品。当我尝试构建时,我得到了以下错误:
“无法从字符串转换为字符”
它在product_productpage_rec {0}
上绊倒了我错过了什么?
提前致谢。
答案 0 :(得分:6)
sb.AppendFormat("mboxCreate(\"product_productpage_rec{0}\")", i);
应该有效
答案 1 :(得分:3)
更改此
sb.Append("mboxCreate(\"product_productpage_rec{0}\")", i);
对此:
sb.AppendFormat("mboxCreate(\"product_productpage_rec{0}\")", i);
答案 2 :(得分:0)
您需要使用
sb.Append(String.Format("mboxCreate(\"product_productpage_rec{0}\")", i));