コード例 #1
0
ファイル: GiftController.cs プロジェクト: Garibov2001/BuyGift
        public async Task <ActionResult> Edit(GiftEditViewModel data)
        {
            var  imageErrorMsg           = new StringBuilder();
            User current_user            = Session["user"] as User;
            HttpPostedFileBase new_image = Request.Files["NewImage"];
            var cache_gift = await _unitofWork.Gifts.Get(g => g.Id == data.Id && g.UserID == current_user.Id);

            string imagePath = cache_gift.Image;

            if (cache_gift == null)
            {
                return(HttpNotFound());
            }

            if (ModelState.IsValid)
            {
                bool NotError = true;

                if (new_image.ContentLength > 0)
                {
                    if (
                        (_fileHandler.CheckContentType(new_image, new List <string> {
                        "image/jpg", "image/jpeg", "image/svg+xml", "image/png"
                    }, imageErrorMsg) &
                         _fileHandler.CheckSize(new_image, 2 * StorageUnits.Megabyte, imageErrorMsg))
                        )
                    {
                        _fileHandler.Delete(imagePath);
                        imagePath = _fileHandler.Upload(new_image);
                    }
                    else
                    {
                        ModelState.AddModelError("image", imageErrorMsg.ToString());
                        NotError = false;
                    }
                }

                if (NotError)
                {
                    var updated_gift = new Gift
                    {
                        Id       = data.Id,
                        Title    = data.Title,
                        Quantity = data.Quantity,
                        Image    = imagePath,
                        Status   = GiftStatus.InCheckUp,
                        Content  = data.Content,
                        UserID   = current_user.Id
                    };

                    await _unitofWork.Gifts.Update(updated_gift);

                    return(RedirectToAction("List", "Gift"));
                }
            }


            return(View());
        }
コード例 #2
0
ファイル: GiftController.cs プロジェクト: Garibov2001/BuyGift
        public async Task <ActionResult> Edit(int?id)
        {
            User current_user = Session["user"] as User;
            var  gift         = await _unitofWork.Gifts.Get(g => g.Id == id && g.UserID == current_user.Id);


            if (gift == null)
            {
                return(HttpNotFound());
            }

            var model = new GiftEditViewModel
            {
                Id           = gift.Id,
                Title        = gift.Title,
                Quantity     = gift.Quantity,
                CurrentImage = _fileHandler.GetFileUrl(gift.Image, this.Url),
                Content      = gift.Content,
            };


            return(View(model));
        }