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

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


这是我的控制器实用程序Meyhod(不执行操作):

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


foreach方法的ReturnProductsByCategoryId循环中,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]时,它在循环的第一次显示该错误。如何解决?有任何想法吗?提前致谢。

最佳答案

这个

SubCategoriesProduct = new List<BsSingleCategoryProductModel>()


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

对于您而言,您可以只执行Add

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

关于c# - 数据不在循环中保存在ModelList变量中。 “索引超出范围”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23873489/

10-12 12:23
查看更多