Esempio n. 1
0
        public void CreateCategory(Category category, int[] selected, int[] selected2, HttpPostedFileBase image)
        {
            Category newcategory = category;

            //загрузка изображения
            if (image != null)
            {
                newcategory.ImageMimeType = image.ContentType;
                newcategory.Image         = new byte[image.ContentLength];
                image.InputStream.Read(newcategory.Image, 0, image.ContentLength);
            }

            //загрузка типов категорий
            newcategory.CategoryTypes.Clear();
            if (selected != null)
            {
                foreach (CategoryType item in CategoryTypes.Where(item => selected.Contains(item.Id)))
                {
                    newcategory.CategoryTypes.Add(item);
                }
            }

            //загрузка родительских категорий
            newcategory.ParentCategories.Clear();
            if (selected2 != null)
            {
                foreach (Category item in Categories().Where(item => selected2.Contains(item.Id)))
                {
                    newcategory.ParentCategories.Add(item);
                }
            }

            dbcontex.Categories.Add(newcategory);
            dbcontex.SaveChanges();
        }
Esempio n. 2
0
        public void SaveEditedCategory(Category category, int[] selected, int[] selected2, HttpPostedFileBase image)
        {
            Category newcategory = FindCategory(category.Id);

            newcategory.Title       = category.Title;
            newcategory.Description = category.Description;

            //загрузка изображения
            if (image != null)
            {
                newcategory.ImageMimeType = image.ContentType;
                newcategory.Image         = new byte[image.ContentLength];
                image.InputStream.Read(newcategory.Image, 0, image.ContentLength);
            }

            newcategory.CategoryTypes.Clear();
            if (selected != null)
            {
                foreach (CategoryType item in CategoryTypes.Where(item => selected.Contains(item.Id)))
                {
                    newcategory.CategoryTypes.Add(item);
                }
            }

            newcategory.ParentCategories.Clear();
            if (selected2 != null)
            {
                foreach (Category item in Categories().Where(item => selected2.Contains(item.Id)))
                {
                    newcategory.ParentCategories.Add(item);
                }
            }

            dbcontex.Entry(newcategory).State = EntityState.Modified;
            dbcontex.SaveChanges();
        }