Exemplo n.º 1
0
        public ActionResult ArticleEdit(int ArticleId)
        {
            ArticleForEdit articleForEdit;
            if (ArticleId!=0)
            {
                Article article = repository.Articles_allImages
                          .Where(x => x.ArticleID == ArticleId)
                          .FirstOrDefault();
                articleForEdit = new ArticleForEdit(article);
            }
            else
            {
                articleForEdit = new ArticleForEdit();
                articleForEdit.Images = new List<Image>();
            }

            //**Создание выпадающего списка
            EFCategoryRepository category = new EFCategoryRepository();
            SelectList DropDownList = new SelectList(category.Categorys, "CategoryID", "Name",
                                                  articleForEdit.Category_CategoryID.ToString());
            ViewData["Category"] = DropDownList;

            return View(articleForEdit);
        }
Exemplo n.º 2
0
        public ActionResult ArticleEdit(ArticleForEdit articleForEdit,
                                        HttpPostedFileBase introimgChang,
                                        HttpPostedFileBase[] image,
                                        int category = 0)
        {
            if (articleForEdit.ArticleID != 0)
            {
                Article article = repository.Articles_allImages
                                            .Where(x => x.ArticleID == articleForEdit.ArticleID)
                                            .FirstOrDefault();
                articleForEdit.IntroImg = article.IntroImg;
                articleForEdit.Images = article.Images;
            }
            else
            {
                articleForEdit.IntroImg = new Image();
                articleForEdit.Images = new List<Image>();
            }
            articleForEdit.Category_CategoryID = category;
            bool isImageNull = false;
            int OldImagesCount = articleForEdit.Images.Count;
            bool TextIsValid = true;
            if (image != null)
            {
                for (int i = 0; i < image.Count(); i++)
                {
                    if (image[i] == null && OldImagesCount < i)
                        isImageNull = true;
                }

                try { string.Format(articleForEdit.Text, image); }
                catch (Exception) { TextIsValid = false; }
            }
            bool isValid = ModelState.IsValid;
            if (isValid)
                if (TextIsValid)
                    if (!isImageNull)
                        if (articleForEdit.IntroImg != null || introimgChang != null)
                        {

                            if (introimgChang != null)
                            {
                                int introimgLength = introimgChang.ContentLength;
                                Image imageTmp = new Image()
                                {
                                    ImageType = introimgChang.ContentType,
                                    ImageData = new byte[introimgLength],
                                };
                                introimgChang.InputStream.Read(imageTmp.ImageData, 0, introimgLength);

                                articleForEdit.IntroImg = imageTmp;
                            }
                            if (articleForEdit.Images == null)
                                articleForEdit.Images = new List<Image>();

                            if (image!=null && OldImagesCount <= image.Count())
                            {
                                for (int i = 0; i < image.Count(); i++)
                                    if (image[i] != null)
                                    {
                                        Image imageTmp = new Image()
                                        {
                                            ImageType = image[i].ContentType,
                                            ImageData = new byte[image[i].ContentLength],
                                        };
                                        image[i].InputStream.Read(imageTmp.ImageData, 0, image[i].ContentLength);

                                        if (OldImagesCount <= i)
                                            articleForEdit.Images.Add(imageTmp);
                                        else
                                            articleForEdit.Images[i] = imageTmp;
                                    }
                            }
                            else
                            {
                                //articleForEdit.Images.RemoveRange(articleForEdit.Images.Count - 1,
                                //    articleForEdit.Images.Count - image.Count());
                                //for (int i = 0; i < articleForEdit.Images.Count; i++)
                                //    if (image[i] != null)
                                //    {
                                //        Image imageTmp = new Image()
                                //        {
                                //            ImageType = image[i].ContentType,
                                //            ImageData = new byte[image[i].ContentLength],
                                //        };
                                //        image[i].InputStream.Read(imageTmp.ImageData, 0, image[i].ContentLength);

                                //        articleForEdit.Images[i] = imageTmp;
                                //    }
                            }
                            repository.SaveArticle(articleForEdit, "123123");// User.Identity.Name);
                            int i;
                            i = 8;
                            return RedirectToRoute(new
                            {
                                controller = "Article",
                                action = "TheArticle",
                                ArticleId = articleForEdit.ArticleID
                            });
                        }

            //**Создание выпадающего списка
            EFCategoryRepository categoryRepo = new EFCategoryRepository();
            SelectList DropDownList = new SelectList(categoryRepo.Categorys,
                                                    "CategoryID", "Name",
                                                    articleForEdit.Category_CategoryID.ToString());
            ViewData["Category"] = DropDownList;
            return View(articleForEdit);
        }