コード例 #1
0
        public ActionResult Edit(int id)
        {
            var content = Db.ContentLibraries.FirstOrDefault(c => c.Id == id);

            if (content == null)
            {
                return(new HttpNotFoundResult("Content item was not found"));
            }
            var model = new ContentLibraryEditModel
            {
                ContentType       = content.ContentType,
                Featured          = content.Featured,
                Id                = content.Id,
                CanDeleteImage    = !content.ImageUrl.IsNullOrWhiteSpace(),
                ImageUrl          = MakeImageUrl(content.ImageUrl, false),
                Precis            = content.Precis,
                Title             = content.Title,
                Url               = content.Url,
                Status            = content.Status,
                AvailableFromDate = content.AvailableFromDate,
                TimeZone          = content.TimeZone
            };

            if (model.Title.IsNullOrWhiteSpace())
            {
                ModelState.AddModelError("Title", "Title cannot be left empty");
            }
            if (model.Url.IsNullOrWhiteSpace())
            {
                ModelState.AddModelError("Url", "Url cannot be left empty");
            }

            return(View(model));
        }
コード例 #2
0
        public ActionResult Edit(ContentLibraryEditModel model)
        {
            ProcessImageUpload imageData = new ProcessImageUpload
            {
                PostedFile = model.UploadedFile,
            };

            if (!imageData.CheckImageOk())
            {
                ModelState.AddModelError("UploadedFile", imageData.Error);
                return(View(model));
            }

            if (imageData.Empty && !model.DeleteImage && !ModelState.IsValid)
            {
                return(View(model));
            }

            var content = Db.ContentLibraries.FirstOrDefault(c => c.Id == model.Id);

            if (content == null)
            {
                return(new HttpNotFoundResult("Content item was not found"));
            }
            if (model.DeleteImage)
            {
                if (DeleteImage(content.ImageUrl))
                {
                    content.ImageUrl = "";
                }
            }
            else
            {
                if (!imageData.Empty && imageData.PostedFileSaveAndResize(Server.MapPath(AppSettings.ContentLibraryImagePath), "item" + model.Id))
                {
                    content.ImageUrl = imageData.SavedName;
                }
            }
            //NOTE: model.sendDateTime will be in local time. We need it in the timezone the user has selected
            var correctDateTime = model.AvailableFromDate;

            if (correctDateTime.HasValue)
            {
                var timezone = System.TimeZoneInfo.FindSystemTimeZoneById(model.TimeZone);
                var newDate  = System.TimeZoneInfo.ConvertTimeToUtc(model.AvailableFromDate.Value, timezone);

                correctDateTime = newDate;
            }


            content.Title             = model.Title;
            content.ContentType       = model.ContentType;
            content.Precis            = model.Precis;
            content.Url               = model.Url;
            content.Featured          = model.Featured;
            content.AvailableFromDate = model.AvailableFromDate != null ? correctDateTime : DateTime.UtcNow;
            Db.SaveChanges();
            return(!imageData.Empty || model.DeleteImage ? RedirectToAction("Edit", new { id = model.Id }) : RedirectToAction("Index"));
        }