コード例 #1
0
        public async Task <IActionResult> Edit(Guid id, MerchantVoucherViewModel merchantVoucher,
                                               [FromForm(Name = "ImageFile")] IFormFile file)
        {
            if (id != merchantVoucher.Voucher.Id)
            {
                return(NotFound());
            }
            var user = await _userManager.GetUserAsync(User);

            // TODO: clean this mess up. I couldnt get the category to bind to the voucher model.
            // So i added the id to the viewmodel and added it by hand here.
            merchantVoucher.Voucher.Category =
                _context.VoucherCategories.FirstOrDefault(x => x.Id == merchantVoucher.CategoryId);
            if (ModelState.IsValid)
            {
                if (merchantVoucher.Voucher.DefaultImages == DefaultImages.Default)
                {
                    await _voucherService.UpdateMerchantVoucher(merchantVoucher.Voucher, file);
                }
                else
                {
                    await _voucherService.UpdateMerchantVoucher(merchantVoucher.Voucher,
                                                                merchantVoucher.Voucher.DefaultImages);
                }


                return(RedirectToAction(nameof(Index)));
            }

            return(View(merchantVoucher));
        }