Beispiel #1
0
        private void UploadModelImage(ModelImageViewModel viewModel, HttpPostedFileBase thumbnail, HttpPostedFileBase image, lnkmodelimage modelImage)
        {
            if (thumbnail != null)
            {
                SaveImage(viewModel, thumbnail);
                modelImage.Thumbnail = Path.GetFileName(thumbnail.FileName);
            }

            if (image != null)
            {
                SaveImage(viewModel, image);
                modelImage.Image = Path.GetFileName(image.FileName);
            }
        }
Beispiel #2
0
        public virtual ActionResult UploadImage(ModelImageViewModel viewModel, HttpPostedFileBase fileMain, HttpPostedFileBase fileSub,
            HttpPostedFileBase fileThumbnail1, HttpPostedFileBase fileThumbnail2, HttpPostedFileBase fileThumbnail3, HttpPostedFileBase fileThumbnail4,
            HttpPostedFileBase fileImage1, HttpPostedFileBase fileImage2, HttpPostedFileBase fileImage3, HttpPostedFileBase fileImage4)
        {
            try
            {
                using (var context = new TTTEntities())
                {
                    var modelColourDesc = context.lnkmodelcolourdescs.Where(a => a.ID == viewModel.ModelColourDescID).Single();
                    var modelImages = context.lnkmodelimages.Where(a => a.ModelColourDescID == viewModel.ModelColourDescID).ToList();

                    if (fileMain != null)
                    {
                        SaveImage(viewModel, fileMain);
                        modelColourDesc.MainImage = Path.GetFileName(fileMain.FileName);
                    }

                    if (fileSub != null)
                    {
                        SaveImage(viewModel, fileSub);
                        modelColourDesc.SubImage = Path.GetFileName(fileSub.FileName);
                    }

                    for (int i = 0; i < 4; i++)
                    {
                        var modelImage = new lnkmodelimage()
                        {
                            CreateDT = DateTime.Now,
                            Active = true,
                            ModelColourDescID = viewModel.ModelColourDescID,
                        };

                        //existing model images
                        if (modelImages.Count() > i)
                            modelImage = modelImages[i];

                        switch (i)
                        {
                            case 0:
                                UploadModelImage(viewModel, fileThumbnail1, fileImage1, modelImage);
                                break;
                            case 1:
                                UploadModelImage(viewModel, fileThumbnail2, fileImage2, modelImage);
                                break;
                            case 2:
                                UploadModelImage(viewModel, fileThumbnail3, fileImage3, modelImage);
                                break;
                            case 3:
                                UploadModelImage(viewModel, fileThumbnail4, fileImage4, modelImage);
                                break;
                        }

                        // new model images
                        if (modelImage.ID == 0)
                            context.lnkmodelimages.Add(modelImage);

                    }
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return RedirectToAction("Edit", new { id = viewModel.ModelColourDescID }); 
        }