gpt4 book ai didi

c# - 数据未在循环中保存在 ModelList 变量中。 "Index was out of range"

转载 作者:太空宇宙 更新时间:2023-11-03 21:31:40 26 4
gpt4 key购买 nike

我知道这是一个常见问题,但我找不到原因。

模型 1:

public class BsSingleCategoryProductModel
{
public BsSingleCategoryProductModel()
{
Products = new List<ProductOverviewModel>();
}
public int Id { get; set; }
public string Name { get; set; }
public string SeName { get; set; }
public IList<ProductOverviewModel> Products { get; set; }
}

模型 2:

public class BsMultipleCategoryProductModel
{

public BsMultipleCategoryProductModel()
{

SubCategories = new List<Category>();
SubCategoriesProduct = new List<BsSingleCategoryProductModel>();
}
public int Id { get; set; }
public string Name { get; set; }
public string SeName { get; set; }
public IList<Category> SubCategories { get; set; }
public IList<BsSingleCategoryProductModel> SubCategoriesProduct { get; set; }
}

这是我的 Controller Utility Meyhod(不是 Action):

public string ReturnProductsByCategoryId(int categoryId)
{


var subcategories = _categoryService.GetAllCategoriesByParentCategoryId(categoryId, true).ToList();



if (subcategories.Count > 0)
{
var category = _categoryService.GetCategoryById(categoryId);
var model = new BsMultipleCategoryProductModel();
model.SubCategories = subcategories;
model.Id = category.Id;
model.Name = category.Name;
model.SeName = category.GetSeName();

int i = 0;
foreach (var subcategory in subcategories)
{
model.SubCategoriesProduct[i] = PrepareProductsInCategory(subcategory.Id);
i++;
}


return RenderPartialViewToString("AllProductsInCategoryWithSubcategory", PrepareProductsInCategory(categoryId));

}else
{
return RenderPartialViewToString("AllProductsInCategory", PrepareProductsInCategory(categoryId));
}

}

这是另一种方法:

public BsSingleCategoryProductModel PrepareProductsInCategory(int categoryId)
{
var model = new BsSingleCategoryProductModel();
var category = _categoryService.GetCategoryById(categoryId);
var categoryIds = new List<int>();
categoryIds.Add(categoryId);
IPagedList<Product> products = new PagedList<Product>(new List<Product>(), 0, 1);
products = _productService.SearchProducts(categoryIds: categoryIds,
storeId: _storeContext.CurrentStore.Id,
visibleIndividuallyOnly: true);


model.Id = category.Id;
model.Name = category.Name;
model.SeName = category.GetSeName();
model.Products = PrepareProductOverviewModels(products).ToList();

return model;
}

ReturnProductsByCategoryId 方法的 foreach 循环中 model.SubCategoriesProduct[i] 出现该错误。

索引超出范围。必须为非负且小于集合大小。参数名称:index

我进行了调试,值生成正确。每当值试图插入 model.SubCategoriesProduct[i] 时,它会在循环的第一次显示该错误。如何解决?有任何想法吗?提前致谢。

最佳答案

这个

SubCategoriesProduct = new List<BsSingleCategoryProductModel>()

创建一个长度为 0 的列表。这意味着 SubCategoriesProduct[i] 将对任何 i 失败,因为它试图访问没有元素的列表的元素。

对于您的情况,您可以改为执行 Add:

model.SubCategoriesProduct.Add(PrepareProductsInCategory(subcategory.Id));

关于c# - 数据未在循环中保存在 ModelList 变量中。 "Index was out of range",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23873489/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com