数据没有保存在循环上的modelist变量中.指数超出了范围

本文关键字:指数 变量 范围 modelist 保存 存在 循环 数据 | 更新日期: 2023-09-27 18:05:07

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

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

这是我的控制器实用方法(不是动作):

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 was out of range. Must be non-negative and less than the size of the collection.Parameter name: index

我做了调试,值生成正确。每当该值试图插入model.SubCategoriesProduct[i]时,它就会在循环的第一次显示错误。如何解决这个问题?什么好主意吗?

数据没有保存在循环上的modelist变量中.指数超出了范围

This

SubCategoriesProduct = new List<BsSingleCategoryProductModel>()

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

对于您的情况,您可以直接使用Add:

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