Skip to main content
 首页 » 编程设计

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

2024年02月13日11落叶无声

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

模型 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));